Getting Started Docker

What is Docker

Docker simply say is like a virtual machine but much lighter. If you want to for example do some experiment on ros without being worried your own computer files are changed, you can use ros docker. That means you can run ros on docker as if you are running it on a separate machine. You can easily remove it afterward, as if you removed a folder in your computer. Docker is like your computer, so you can install anything in it.

You can also use existing docker and make your own docker based on it. So you get all the installed programs.

Docker setting up

Install docker:

# this is to remove old apt repo if needed
sudo rm /etc/apt/sources.list.d/docker.list{,.save}
curl -sSL https://get.docker.com/ | sh
sudo usermod -aG docker $USER # reboot to apply, after this, you don't need to run sudo before docker commands anymore

For having Nvidia works properly on your computer follow the instruction below (only for ubuntu Xenial x86–64) or the link below.
https://github.com/NVIDIA/nvidia-docker

# Remove old installation.
docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo apt-get purge -y nvidia-docker
# this command is not mentioned in the website
sudo rm -r /var/lib/nvidia-docker
# Add the package repositories
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
# Install nvidia-docker2 and reload the Docker daemon configuration
sudo apt-get install -y nvidia-docker2
# This is instead of `sudo pkill -SIGHUP dockerd`
sudo service docker restart
# Test nvidia-smi with the latest official CUDA image
docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi

After that you can you either `docker`

Docker basic commands

Get the docker:

# either from dockerfile
docker build dockerfile
# or pull it from repository
docker pull repository

Lots of docker images are located in docker hub. Below you can get ros or keras in a docker.

# get keras: https://hub.docker.com/r/gw000/keras/
docker pull gw000/keras-full
# get ros: https://hub.docker.com/_/ros/
docker pull ros

> docker image is obtained from docker file or pulling it. Each image can have many container. Container is sort of your private operating system with software installed originally from image.

# get list of images on your computer
docker images
# get list of containers on your computer
docker ps -a
# remove image
docker rmi your-image-name-or-id
# remove container
docker rm your-container-name-or-id

To make a container and reuse it:

# make a container and start it for the first time. Remove ` — rm`, if you want to reuse it. Here random container name is given. See it by `docker ps -a`. You can give your own name by using ` — name your-container-name` in the exec command. 
docker exec -it --rm your-image-name /bin/bash
## reuse container
# you don’t need start if your container is running
docker start your-container-name
# reuse it
docker run -it your-container-name /bin/bash
# stop it
docker stop your-container-name

Docker list of commands

# get list of all commands
docker -h
# get specific command help
docker image -h
# or this one if -h doesn’t exist
docker image --help

Docker example

`-v` o`--volume` is to share your computer folders in docker. The rest of the rick is here.

#only needed if you want to open a GUI. It stays valid until shutdown/reboot of your host computer.
xhost +local:docker
docker run --runtime=nvidia -it --rm --volume="/your/test1/folder:/root/test1/on/docker" --volume="/your/test2/folder:/root/test2/on/docker" -v "/your/test3/folder:/root/test3/on/docker" --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" ros:latest /bin/bash

The --env="QT_X11_NO_MITSHM=1" not needed.

There are other options such as:

--name="my_docker"
--env="XDG_RUNTIME_DIR" \
--env="QT_X11_NO_MITSHM=1" \
--device=/dev/dri:/dev/dri \
--device=/dev/input:/dev/input \
-v "/etc/localtime:/etc/localtime:ro" \
-v "/etc/group:/etc/group:ro" \
-v "/etc/passwd:/etc/passwd:ro" \
-u `id -u`:`id -g` \
-v "xenial_dev_home:$HOME" \
--workdir $HOME/ \
--net=host \
--privileged \

Troubleshooting

Run docker without sudo

If you need don’t have administration right on docker, you can add the user to the group docker by this command.

sudo usermod -aG docker $USER
sudo service docker restart # or simply reboot the computer

Make image from container

If you forgotten to mount specific place in your computer, you can make an image from your container and mount the new folder plus previous settings.

docker commit your_container new_image

Nvidia is not recognize in docker as runtime option

In the new version of docker, to run nvidia, --runtime=nvidia should be given. If there is a problem, check in /etc/docker/daemon.json , that contains the following command

{
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}

Weird behavior in terminal

When having problems with the spacing of the lines and columns (the makeup in the terminal seems weird, corrupted), try this:

docker exec -it mytensorflow bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash"
# or instead simply give the environment variable:
docker exec -it -e COLUMNS=$COLUMNS -e LINES=$LINES mytensorflow bash

docker takes time to restart

You can check error list from journalctl. So far no solution for it. You just need to wait. The problem is reported (https://github.com/NVIDIA/nvidia-docker/issues/604).

sudo journalctl -u docker.service -f