Docker is a useful tool for creating small virtual machines called containers. Containers are instances of docker images, which are defined in a simple language. This language is usually written in a file named Dockerfile and it’s common practice to version control these files. When you run a container on your computer you get access to an entirely separate Linux environment. Better yet, you can run the same container on your laptop that runs in a battle-tested production environment, giving you the opportunity to develop and test in a realistic setting. This takes one major source of uncertainty out of the process of running your code on another machine.

Using Docker on a Mac is easy (the process is similar-ish on Windows, depending on the actual machine). Here are a few steps to get started.

Build and launch the container

  1. Install the latest stable client here.
  2. Create a folder called hello-docker and create a file called Dockerfile in it with the following:

    FROM jupyter/minimal-notebook
    CMD --NotebookApp.token=''
  3. Navigate to the hello-docker folder in your terminal and run the following:

    docker build -t insecure-notebook .
  4. Next, start your container:

    docker run -d -p 8888:8888 insecure-notebook

    NOTE: if you have a Jupyter notebook running on port 8888 you should shut it down first. Alternatively, you can change the port before the colon. For example, -p 8889:8888 will use port 8889 on your machine to connect to port 8888 in the Docker container.

  5. Navigate to localhost:8888 in your browser. Welcome to your container-hosted Jupyter notebook!

Take the container down

  1. Run docker ps to see which containers are running. You should see something like this:

     >>> docker ps
     CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
     5ccfea1e3720        insecure-notebook   "tini -- /bin/sh -..."   4 minutes ago       Up 4 minutes>8888/tcp   vigilant_austin
  2. Copy the container ID of your notebook.
  3. Kill and delete the container:

    docker rm -f <container ID>

    For me, this looks like:

    docker rm -f 5ccfea1e3720