Installing and using Docker
Table of contents
What is Docker
To understand Docker, it is necessary to understand containers. A container is a method of bundling together an entire runtime environment for an application, decoupling it from the application itself. They are a bit like a virtual machine, but there is no guest operating system, as the container sits right on top of your own host operating system. Docker has a nice diagram for this in their documentation describing containers here. Using containers brings many benefits, but in essence, you get an isolated consistent environment to run your application from that is lightweight and easy to share.
Docker is by far the most popular container format out there. It is open-source, and supported by many other platforms, such as Google Cloud. Docker is free to use for individuals and developments teams for public repositories, and can be paid for to get increased benefits, such as parallel continuous integration builds.
How to set up Docker
- Install Docker following the instructions on the official website.
-
Test your installation
sudo docker run hello-world sudo docker image ls
- There is a reference here for the run command.
- Get started with an actual docker image to test.
- For example, you could try the nvidia docker image, or create your own container following the steps below.
Creating a docker container
- Find a docker container to use as a base at Docker Hub. For example, here is the image for Python.
-
Create a file called
dockerfile
(with no extension), place it in your code repository, and add the following to it.# Firstly, FROM, the location in step 1, python for example FROM python:3 # What the working directory in the image filesystem (not the host) should be WORKDIR /usr/src/app # COPY the relevant files # This operation obeys a .dockerignore (same rules as a git ignore) COPY . . # Set any required environment variables, for e.g # ENV PIP_NO_CACHE_DIR=1 # Install anything needed RUN python -m pip --no-cache-dir install . # Specify the command the container should run by default CMD ["python", "./cli.py"]
-
Build and run the container with the commands from the directory the dockerfile is in
docker build --tag my_python_app:1.0 . docker run -it --rm --name my_running_name my_python_app:1.0
Share your image on Docker Hub
- Obtain a Docker ID.
- Login to Docker
docker login
. - Tag the image
docker tag local-image:tagname new-repo:tagname
. - Push the image to Docker Hub
docker new-repo:tagname
.
Useful commands
-
Removing stopped containers
docker rm $(docker ps -q -a)
-
Running GUI applications - mount the socket for the X server as a Docker volume
docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY TheImage
-
Running a PyQT GUI application
Firstly, add the following lines to your dockerfile to install the required packages
RUN apt-get update RUN apt-get install python3-pyqt5 -y
Then use the following commands to run your image
echo "allowing root to connect to the X server" xhost local:root echo "Disallowing QT to access shared memory" export QT_X11_NO_MITSHM=1 echo "Running the image" docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY TheImage
-
How to mount data from your local filesystem
docker run --mount type=bind,source=/home/username/my-data,target=/mnt/my-data
-
Launch an interactive bash session
docker run -it <image> bash
Further considerations
By default, docker stores images in /var
on linux.
If this is not preferred, the best guide I have found to change this is available here.
Alternatively, you could keep docker on /var
and regularly clean it with
docker system prune -a
You may need to run all docker commands as sudo. The easiest fix for this seems to be the below. Though you will need to run it occasionally to keep the setting.
sudo chmod 666 /var/run/docker.sock
Further reading
- Guides for using TensorFlow and PyTorch with Docker.
- Bind mounts and volumes in Docker.
- Move Windows path to Docker data (why this is not an option from the program, I’m not sure!).
(Photo by Tj Holowaychuk on Unsplash)