This is an old revision of the document!
Table of Contents
Tutorial on Using Docker
What is Docker?
Docker is a platform that helps with easy deployment of applications inside a container. Containers are very similar to virtual machines but are getting more popular due to theor flexibility and ease of use. Some key differences
Image vs container
A container is created by running an image. An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.
A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.
Container vs virtual machine
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
Preparing Docker Environment
Installation
Install Docker-CE community edition by following the instructions here https://docs.docker.com/install/linux/docker-ce/ubuntu/
There are three ways to install docker 1. Using docker repositories via “sudo apt” 2. Manual installation using .deb file and update when needed 3. Using convenience scripts - MOtsly for dev/test environments needing automation
We go with first option “INSTALLING USING REPOSITORIES”. Follow this section in the link above. Do not forget to go thorugh Post installation instructions here : https://docs.docker.com/install/linux/linux-postinstall/ This is useful to use docker without having to specify “sudo”.
Test Installation
Test for successful installation of Docker by executing folowing commands
Docker --version
This should give an output as shown below
Docker version 19.03.1, build 74b1e89e8a
Now execute
Docker run hello-world
This command downloads and runs a test image. This should produce the following output
latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:451ce787d12369c5df2a32c85e5a03d52cbcef6eb3586dd03075f3034f10adcd Status: Downloaded newer image for hello-world:latest
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
You can also run which gives more information
docker info
Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 17.12.0-ce Storage Driver: overlay2 ...
NVIDIA Docker
This is useful to use docker image with nvidia runtime. we can now create GPU accelerated containers and run applications inside. This avoids the need to install the CUDA/GPU driver inside the containers and have it match the host kernel module. Instead, drivers are on the host and the containers don't need them.https://www.nvidia.com/object/docker-container.html
Note : The latest version is nvidia-container-toolkit. You can install that and see its usage from the official github page here https://github.com/NVIDIA/nvidia-docker But, at the time of making this document nvidia-docker2 was being used. For now nvidia-docker2 can be upgraded as mentioned on the github page and supports all the cli options as nvidia-container-toolkit.
A container can be run using –runtime=nvidia option as follows
docker run --runtime=nvidia nvidia/cuda:9.0-cudnn7-devel nvidia-smi
In the above command we are running an image named “cuda:9.0-cudnn7-devel” from nvidia repository and then executes the command “nvidia-smi” inside it. If the nvidia-docker installation is correct it should produce the output showing the nvidia-driver details from inside the docker container.