Skip to content

Instantly share code, notes, and snippets.

@chrisamiller
Last active May 28, 2024 16:11
Show Gist options
  • Save chrisamiller/5740929b50de2030eee316b315070f6a to your computer and use it in GitHub Desktop.
Save chrisamiller/5740929b50de2030eee316b315070f6a to your computer and use it in GitHub Desktop.

Using Docker

On your laptop:

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" 

What's cool is that didn't run on MacOS, that ran in Linux, and we can prove it:

docker run ubuntu uname -a

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

There is one major difference between running docker on the cluster and on your laptop:

whoami

We can't get root access in docker images on the cluster. To oversimplify a complicated topic, that's to prevent people from accessing data they shouldn't be able to.

Here though, we have root, so let's install some software.

python3

fails - it's not installed!

apt-get update
apt-get install python3

Now it should work: python3 >>> print("Hello World!")

Now, exit out python and your container

<CTRL-D to quit python >
exit 

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

My First Dockerfile

Create a folder

mkdir ubuntu-python
    cd ubuntu-python

Create a new text file named "Dockerfile" with the following contents:

# start from base ubuntu
FROM ubuntu:latest

MAINTAINER Chris Miller <c.a.miller@wustl.edu>

RUN apt-get -y update
RUN apt-get -y install python3

Save that file in the directory And build a docker image from that Dockerfile

    cd ..
    docker build -t chrisamiller/ubuntu-python ubuntu-python/

Let's run it:

    docker run -it chrisamiller/ubuntu-python

and verify that python is installed

    python3

Great, so now let's run a python script on our data. First let's find our data by listing our home directory:

    ls -l /Users/cmiller  #replace with your path

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 /Users/cmiller/workshop:/data -it chrisamiller/ubuntu-python

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

ls -l /data/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment