Skip to content

Instantly share code, notes, and snippets.

@chrisamiller
Created November 17, 2024 21:21
Show Gist options
  • Select an option

  • Save chrisamiller/fab79aa56b259e01744330e1d245085e to your computer and use it in GitHub Desktop.

Select an option

Save chrisamiller/fab79aa56b259e01744330e1d245085e to your computer and use it in GitHub Desktop.

Using Docker

On your cloud instance:

docker pull ubuntu

What is that doing? It's going to https://hub.docker.com/_/ubuntu and pulling down the image with the "latest" tag

docker run ubuntu

Uhh, nothing happened. Not quite - it loaded up the entire OS, but you didn't tell it to do anything!

docker run ubuntu echo "hello world" 

But what if we want to do more than one thing at a time? Run docker interactively! (bash is the most common shell that we generally work on)

docker run -it ubuntu /bin/bash

Great, let's try to use python to parse some data:

python3

That fails - it's not installed! So let's install it:

apt-get update
apt-get install python3

Now it should work:

python3

print("Hello World!")

Now, exit out of python and your container

<CTRL-D to quit python >
exit 

Okay, let's pop back into our container using the above docker run command, then try running python again:

python3

Wait! where'd it go? (Let's chat about persistence)

Okay, well maybe we don't need python right now anyway. Let's go look at some of our course data:

    ls -l /workspace

Wait a minute - there's nothing there... That's because Docker processes are isolated from the rest of your computer. By default, nothing can get in or out. But we probably need to run it on some data, and probably want to save those results when we're done!

In order to do so, we need to mount paths as volumes within the docker container. Let's say I have a directory at ~/workshop. I'd like to be able to view that within my docker container. No problem, we'll use the volumes option to mount it, creating a virtual "tunnel" through the docker container's isolation. This uses the -v flag and a colon separated list of source (local path) and destination (container path)

    docker run -v /workspace:/data -it ubuntu /bin/bash

So we mounted it at the root in a data folder. Let's check to see if our expected data is there:

ls -l /workspace

??? Oh, look - we mounted it at /data/ instead of /workspace, so from the perspective of the docker container, that's where it lives!

ls -l /data

Lists our data as we expect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment