Docker installation and GPU support

1. Docker installation process (ubuntu18.04 environment)

Tsinghua mirror docker installation: docker-ce | mirror station use help | Tsinghua University open source software mirror station | Tsinghua Open Source Mirror

1. Since the docker version in the apt official library may be relatively old, first uninstall the old version that may exist:

sudo apt-get remove docker docker-engine docker-ce docker.io

2. Update the apt source index of ubuntu

sudo apt-get update

3. Configure the installation package to allow apt to use the warehouse through HTTPS

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

4. Add Docker official GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

5. Set up the Docker stable version warehouse

sudo add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

6. Update the apt source index again

sudo apt-get update

7. Install the latest version of Docker CE (Community Edition)

sudo apt-get install docker-ce

8. If you want to install the specified version of docker, do as follows (you can skip this step if you don’t need it)

apt-cache madison docker-ce    # 列出可用的docker-ce版本
sudo apt-get install docker-ce=18.06.1~ce~3-0~ubuntu    #安装指定的docker版本

9. Pull the hello-world image to test the docker container

sudo docker run hello-world

If an error is reported in this step: (Unable to find image 'hello-world:latest' locally)

If the following information appears, it means that docker is successfully installed and running normally, please refer to this article

Finally, if the following information appears, it means that docker is successfully installed and running normally

 10. Start the Docker service and set it to start at boot:

sudo systemctl start docker    # 启动
sudo systemctl stop docker     # 停止
sudo systemctl restart docker  # 重启
sudo systemctl enable docker   # 设置开机启动

2. Daoker configures root permissions (optional)

Make docker without root privileges, sudo is required every time

The Docker daemon (Docker daemon process, which can be understood as the background of the entire Docker) is bound to a Unix Socket (Unix socket, a communication method) instead of a TCP port for communication. For Unix Socket, it belongs to the root user by default. If other users want to use it, they must add sudo before each command. In this way, the Docker daemon will always run on the root user.
This is more troublesome. We don't want to add sudo to every command and enter the password again and again. What should we do?
According to the previous article, Unix Socket belongs to the root user by default, so we can adjust the user.
In other words, we create a new Unix user group called docker (of course you can call it another name), and then add a user to this user group, so that when the daemon starts, the Unix Socket used belongs to this user's, not root, then no need for sudo.

Note: Such an approach may cause security issues. For details, please refer to  the document Docker Daemon Attack Surface .

Avoid having to add sudo every time you enter a command

Method 1 (this one tested):

Create a docker user group and add users:

sudo groupadd docker
sudo usermod -aG docker $USER

Log out of your account and log in again to make the changes in user group settings take effect:

If you are using a virtual machine, then you'd better restart the virtual machine to make the setting changes take effect.
If you are using a desktop operating system such as a certain distribution of Linux; MacOS; Windows, etc., completely log out of your account, and then log in again to make it The setting changes take effect.
If you use Linux, you can also use such a command to make the changes take effect:

newgrp docker 

Method 2: Configure user groups (this has not been tested):

sudo usermod -a -G docker $USER     

Check if you don't need sudo:

docker run hello-world

This command will download a test docker use case, and will prompt the results of various tests. If the prompt results are correct, then the configuration is normal!

3. Use Nvidia GPU in Docker

After docker released version 19.03, it becomes convenient to use GPU in docker. docker has added the option to use gpu. Only one option is needed to use the GPU in the container, skipping complex configurations and greatly reducing the difficulty of use.

After docker19.03, it is no longer necessary to install nvidia-docker. It can be used only by installing NVIDIA-CONTAINER-RUNTIME, and supports docker-compose.

1. Install NVIDIA-CONTAINER-RUNTIME

1. Check the supported operating systems and versions, and add sources according to the corresponding options. Ubuntu is based on Debian.

Official instructions Configuration software package warehouse address: Migration Notice | nvidia-container-runtime

2. Then execute the following command:

apt-get install nvidia-container-runtime

3. Check whether nvidia-container-runtime is installed successfully

which nvidia-container-runtime-hook

Then restart the docker daemon

2. Expose the GPU to the container

When running the container, add the --gpu parameter to enable gpu support

Use --gpus to make the GPU accessible on the host when starting the container, and you can configure how many GPUs are used

# 使用所有GPU
docker run -it --rm --gpus all ubuntu nvidia-smi

# 使用两个GPU
docker run --gpus 2 ubuntu  nvidia-smi

# 使用指定 GPU
docker run --gpus '"device=1,2"' ubuntu  nvidia-smi

Expose all GPUs to the container, and call the result returned by "nvidia-smi", which means that doker can call the gpu:

Guess you like

Origin blog.csdn.net/ytusdc/article/details/131854074