Docker Tutorial

Install Docker

  • Install
sudo apt install docker.io
systemctl start docker
docker run hello-world

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.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
  • Permission Configure Add use to docker group
$ cat /etc/group | grep docker
docker:x:138:
$ sudo usermod -aG docker lyn
$ cat /etc/group | grep docker
docker:x:138:lyn

or

cat /etc/group | grep docker # 查找 docker 组,确认其是否存在
groups # 列出自己的用户组,确认自己在不在 docker 组中

# 如果 docker 组不存在,则添加之:
sudo groupadd docker

# 将当前用户添加到 docker 组
sudo gpasswd -a ${USER} docker

# 重启服务
sudo service docker restart

# 切换一下用户组(刷新缓存)
newgrp - docker;
newgrp - `groups ${USER} | cut -d' ' -f1`; # TODO:必须逐行执行,不知道为什么,批量执行时第二条不会生效
# 或者,注销并重新登录
  • Change location /var/lib/docker is the path that docker store, so we can change this path to where we want store. eg:/home/dockerData
service docker stop
mkdir /home/dockerData
mv /var/lib/docker /home/dockerData
ln -s /home/dockerData/docker /var/lib/docker
service docker start

Docker Hub

  • change source list atom /etc/docker/daemon.json if not exsit, create one.
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
or
{
  "registry-mirrors" : [
    "http://ovfftd6p.mirror.aliyuncs.com",
    "http://registry.docker-cn.com",
    "http://docker.mirrors.ustc.edu.cn",
    "http://hub-mirror.c.163.com"
  ],
  "insecure-registries" : [
    "registry.docker-cn.com",
    "docker.mirrors.ustc.edu.cn"
  ],
  "debug" : true,
  "experimental" : true
}

Useful Repo

For test if docker config is right.

docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi

Pytorch Docker

https://github.com/pytorch/pytorch/blob/master/README.md#docker-image

You can also pull a pre-built docker image from Docker Hub and run with docker v19.03+

docker run --gpus all --rm -ti --ipc=host pytorch/pytorch:latest

Please note that PyTorch uses shared memory to share data between processes, so if torch multiprocessing is used (e.g. for multithreaded data loaders) the default shared memory segment size that container runs with is not enough, and you should increase shared memory size either with --ipc=host or --shm-size command line options to nvidia-docker run.

-v hostpath:/dockerpath mount share folder with docker.
--name container_name define custom container name
docker ps show all containers
docker commit <container_id> <image_name> based on a docker create a new one.
-p 8888:8888 port mapping before is host port and the next one is docker port.

Jupyter install

jupyter notebook --generate-config
vim ~/.jupyter/jupyter-notebook-config.py
# 允许root启动
c.NotebookApp.allow_root = True
# 允许远程访问
c.NotebookApp.ip = '*'
# 设置notebook文件夹 this is default start you might change if you want.
c.NotebookApp.notebook_dir = '/eswai/jupyter'
# 设置静态token,这样就不用每次换token了,另一种方法是设置密码password
c.NotebookApp.token = '[自定义token]'

https://blog.csdn.net/eswai/article/details/79437428

Then just start jupyter notebook, and put the link createb by the server.

Using host proxy

docker build --network host .

Run gui on docker

宿主机:
1. In terminal: echo $DISPLAY, 类似返回":0"
2. In terminal: xhost +
Container:
1. vim ~/.bashrc
2. add a line: "export DISPLAY=:0",这里的":0"替换成你在宿主机1步返回的结果.
3. source ~/.bashrc
验证(In container):
1. In terminal: apt-get install xserver-xorg x11-xserver-utils
2. In terminal: xclock
3. 会弹出一个时钟的GUI界面

Reference

-Docker安装指南以及使用GPU