Wiki
Docker Passthrough
Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.
1.USB passthrough

- Method 1 (not highly recommended for beginners):
When creating the container, simply add the required devices in the red section. You can view the names of the desired devices using the following command.
ls /dev
#例如
--device=/dev/tty_USB0
- Method 2 (personally more recommended, no need to remount again; although security is reduced, the probability that others can exploit security permissions to attack you is very, very low—only enterprise servers need to be guarded against):
--privileged
Just add one green line, and then all devices will be mounted in Docker. (The red part does not need to be written.)

2.NVIDIA GPU passthrough
NVIDIA Container Toolkit enables users to build and run GPU-accelerated containers. The toolkit includes a container runtime library and utilities to automatically configure containers to leverage NVIDIA GPUs.

https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
2.1.Install
(If possible, refer to the official documentation when available, as the installation method may be updated.)
2.1.1.Ubuntu
- Configure the repository and update
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list \
&& \
sudo apt-get update
- Install nvidia-docker2
sudo apt-get install -y nvidia-docker2
- Use the nvidia-ctk command to configure the container runtime
sudo nvidia-ctk runtime configure --runtime=docker
- Restart the Docker service:
sudo systemctl restart docker
2.1.2.Fedora
- Configure the production repository:
curl -s -L https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo | \
sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo
- Optionally, configure the repository to use experimental packages:
# 如果是RHEL或者Rocky(DNF4)
sudo dnf config-manager --add-repo https://nvidia.github.io/libnvidia-container/stable/rpm/libnvidia-container.repo
# 如果是Feodra41+(DNF5)
sudo dnf config-manager addrepo --from-repofile=https://nvidia.github.io/libnvidia-container/stable/rpm/libnvidia-container.repo
- Install the NVIDIA Container Toolkit packages:
# 如果是RHEL或者Rocky
sudo dnf install -y nvidia-container-toolkit
# 如果是Fedora
sudo dnf install -y nvidia-container-toolkit
- Install nvidia-docker2 : Use on Fedora
dnfto perform the installation.
sudo dnf install -y nvidia-docker2
- Configure the container runtime using the nvidia-ctk command : This command is used to configure the integration of the NVIDIA Container Toolkit with Docker. The command is as follows:
sudo nvidia-ctk runtime configure --runtime=docker
- Restart the Docker service : After completing the configuration, you must restart the Docker service for the changes to take effect:
sudo systemctl restart docker
After these steps are performed, Docker will use the NVIDIA Container Runtime and support GPU-accelerated container execution. You can usenvidia-smicommand to verify whether the NVIDIA GPU is available in the container.
2.2.Test whether it is available
- Run the NVIDIA CUDA container for testing :
可以使用下面指令进行测试,docker会自动从nvidia/cuda拉取11.0.3-base-ubuntu20.04镜像,并创建一个运行一次即删除的容器。
sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi


3.Configure CUDA and CuDNN in Docker
If you do not need to create a Docker image yourself and directly use an image created by a senior student or someone else, you do not need to read this chapter. The CUDA and cuDNN in the container are completely isolated from the local environment. Whether CUDA is installed locally does not matter, but the NVIDIA driver version must meet the minimum version requirement for CUDA.
Below is the section to revisit after you have learned how to create a container image, if you want to create an image yourself:
https://hub.docker.com/r/nvidia/cuda
On the website above, NVIDIA has already configured CUDA and cuDNN for us, so we do not need to configure them ourselves at all, which is much, much simpler than the traditional approach.
We only need to find the corresponding Docker image as the base package.
A CUDA image has three types, as follows. If we need to compile OpenCV4, then we need to use the devel version of CUDA.
| Image type | Use case | Example tag | Size |
|---|---|---|---|
| base | CUDA runtime libraries only | 12.4.0-base-ubuntu22.04 | ~240MB |
| runtime | Deploy compiled applications (including math libraries) | 12.4.0-runtime-ubuntu22.04 | ~2GB |
| devel | Development environment (including build tools) | 12.4.0-devel-ubuntu22.04 | ~3GB |
Let me give an example. For instance, if I want to use Ubuntu24.04 with CUDA12.6 and CuDNN, then I would choosenvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04This image has both CUDA-devel and CuDNN, and it is also based on Ubuntu24.04.

In the Dockerfile, you can write it like this at the beginning.
# 基于NVIDIA官方CUDA 12.6和CuDNN基础镜像
FROM nvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04
4.Configure Ethernet
--net=host \
Just add this line to the startup parameters. Details are below.