Docker Passthrough
USB passthrough

- Method 1 (not recommended for beginners):
When creating the container, simply add the required devices in the red section here. You can use the following command to check the name of the desired device.
ls /dev
#例如
--device=/dev/tty_USB0
- Method 2 (I personally recommend this one, as it avoids the need to remount. Although security is slightly reduced, the probability of someone exploiting security permissions to attack you is extremely low; such precautions are only necessary for enterprise servers):
--privileged
Just add one line in green, and then all devices will be mounted to Docker. (No need to write the red part.)

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

https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
Install
(Prefer official sources whenever possible, as installation methods may be updated.)
Ubuntu
- Configure the repository and update it.
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-ctkcommand to configure the container runtime.
sudo nvidia-ctk runtime configure --runtime=docker
- Restart the Docker service.
sudo systemctl restart docker
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: Install on Fedora using
dnf.
sudo dnf install -y nvidia-docker2
- Configuring 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 Docker Service: After completing the configuration, you must restart the Docker service for the changes to take effect:
sudo systemctl restart docker
After executing these steps, Docker will use the NVIDIA Container Runtime and support GPU-accelerated container operation. You can use the nvidia-smi command to verify whether the NVIDIA GPU is available in the container.
Testing if it works.
- Run an NVIDIA CUDA container for testing:
You can use the following command to test. Docker will automatically pull the nvidia/cuda:11.0.3-base-ubuntu20.04 image and create a container that runs once and is then deleted.
sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi


Docker configuration for CUDA and cuDNN
If you don't need to create your own Docker image and can directly use an image built by a senior student or someone else, you can skip this section. The CUDA and cuDNN inside the container are completely isolated from your local environment, so it doesn't matter whether you have CUDA installed locally. However, the NVIDIA driver version must meet the minimum requirements for the CUDA version being used.
Below is: If you want to create your own image, you can come back to this section after learning how to create a container image:
https://hub.docker.com/r/nvidia/cuda
In the website above, NVIDIA has already configured CUDA and CuDNN for us, so we don't need to set them up ourselves at all. This is far simpler than the traditional approach.
We just need to find the corresponding Docker image to use as the base package.
There are three types of CUDA images, as shown below. If we need to compile OpenCV4, we must use the devel version of CUDA.
| Mirror type | Applicable Scenarios | Example Tags | Size |
|---|---|---|---|
| base | Only the CUDA runtime library is needed. | 12.4.0-base-ubuntu22.04 | ~240MB |
| runtime | Deploy the compiled application (including the math library) | 12.4.0-runtime-ubuntu22.04 | ~2GB |
| devel | Development environment (including compilation tools) | 12.4.0-devel-ubuntu22.04 | ~3GB |
Let me give you an example: if I want to use CUDA 12.6 and CuDNN on Ubuntu 24.04, then I would choose nvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04. This image includes both CUDA-devel and CuDNN, and it's also based on Ubuntu 24.04.

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