Docker Cheat Sheet for Deep Learning 2019

In our previous Docker related blog: “Is Docker Ideal for Running TensorFlow? Let’s Measure Performance with the RTX 2080 Ti” we explored the benefits and advantages of using Docker for TensorFlow. In this blog, we’ve decided to create a ‘Docker Cheat Sheet’ and best practices that have helped us along the way. We welcome you to have a look, keep this blog as a reference, and if you think we left something out or know of a good tip/trick on Docker that’s saved you, let us know, and we’ll add it to the blog!

Basic Docker Terms You Should Know.

  • Image: Docker images are the foundations of containers. Images are an immutable, ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime.
  • Container: An instance of the image that can be executed as an independent application. The container has a mutable layer that lies on top of the image, dependencies isolated from the host machine.
  • Volume: A volume is a specially-designated directory within one or more containers that bypasses the Union File System. Volumes are designed to persist data, independent of the container’s life cycle.
  • Registry: A storage and content delivery system used for distributing Docker images.
  • Repository: A collection of related Docker images, often different versions of the same application.

Note: There are more terms you may want to pick up on, check the glossary at the Docker website

Launching Your Deep Learning Environment in Docker

Let’s take a look at some common Docker syntax by running a practical example. We’ll break it down piece by piece. Assuming your dependencies are installed, (or you’re using an Exxact deep learning workstation or server) you can fire up a TensorFlow container by entering the following command in a terminal:

nvidia-docker run -it -p 8888:8888 -p 6006:6006 -v ~/mywork:/benchmarks tensorflow/tensorflow:nightly-gpu bash

Breaking Down the Above Docker Command

Docker Syntax
nvidia-docker run The ‘docker run’ command specifies the IMAGE to derive the container from. Note in this case we use ‘nvidia-docker run’ to utilize CUDA powered NVIDIA GPUs.
-it This parameter creates an interactive terminal you can use to interact with your container
-p 8888:8888 Expose port 8888:8888 [HOST:CONTAINER] for Jupyter notebook, effectively maps the container port to the host port using localhost interface (type localhost:8888 in your browser).
-p 6006:6006 Expose port 6006:6006 [HOST:CONTAINER] for Tensorboard, maps the container port to the host port using localhost interface (type localhost:6006 in your browser).
-v ~/mywork:/benchmarks Volume tag, This shares the folder ~/mywork on your host machine to /benchmarks folder inside your container. In generic terms  -v /[host folder]:/[container folder]
tensorflow/tensorflow:nightly-gpu This the image that you want to run. The Docker Hub format is [PUBLISHER]/[IMAGE REPO]:[IMAGE TAG] In our case, we use the image ‘tensorflow’ with ‘nightly-gpu’ image tag (if we didnt specify a image tag, the default tensorflow image will be used).
bash This will create a new Bash session in the container.

Quick Tip: Starting a Stopped/Exited Container

1. Start the Container

docker start [container name or ID]
2. Start an interactive bash within the container
docker exec -it [container name or ID] bash

Deep Learning Servers & Workstations

Working With Docker Containers

Command OPTIONS Explanation

docker container ls [OPTIONS]

-a Show all containers (default shows just running)
-f Filter output based on conditions provided
–format Pretty-print containers using a Go template
–last, -n Show n last created containers (includes all states)
-s Display total file sizes

Docker Hub Commands


docker pull [image name]

Downloads image from Docker Hub.

docker search [keyword]

Search Docker Hub for images.

docker push [user/imagename]

Uploads an image to Docker Hub.

docker login [options] [server]

Login to Docker Hub

Image and Container Information

docker container ls List all running containers.
docker container ls -a List all containers running or stopped
docker image ls list images
docker volume ls list volume
docker logs [container name or ID] Displays the logs from a running container.
docker port [container name or ID] View a list of the ports defined on a container.

Other useful commands when working with Docker images and containers

Docker Command
docker run -it user/image Run an image, create a container and change the current terminal to a terminal within the container.
docker run -p [HOSTPORT:CONTAINERPORT] -d user/image Run an image in detached mode with port forwarding [HOSTPORT:CONTAINERPORT] .
docker attach [container name or ID] Change the command prompt from host to running container.
docker start [container name or ID] Start a container.
docker stop [container name or ID] Stop a container.
docker rm [container name or ID] Remove a container.
docker rmi [image name or ID] Delete an image.
docker tag user/image:tag user/image:newtag Add new tag to an image.
docker exec [container name or ID] shell command Execute a command within a running container.

That’s it for now, we’ve only touched on a few Docker tips and commands that have helped us. What Docker commands do you feel we should have included in this Docker Cheat Sheet? Let us know!

ebook for deep[ learning