User Tools

Site Tools


docker:gpu:use_nvidia_cuda_toolkit_within_a_docker_container

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

docker:gpu:use_nvidia_cuda_toolkit_within_a_docker_container [2025/05/21 15:22] – created peterdocker:gpu:use_nvidia_cuda_toolkit_within_a_docker_container [2025/05/21 16:19] (current) peter
Line 6: Line 6:
  
   * These drivers act as the bridge between the operating system and the NVIDIA GPU hardware, ensuring optimal communication and performance.   * These drivers act as the bridge between the operating system and the NVIDIA GPU hardware, ensuring optimal communication and performance.
 +
 +See: [[Ubuntu:GPU:NVIDIA GPU:Setup|Setup]].
 +
 +----
 +
 +====== Install the NVIDIA Container Toolkit ======
 +
 +This toolkit extends Docker to leverage NVIDIA GPUs fully, ensuring that the GPU capabilities can be used within containers without any hitches.
 +
 +===== Download the NVIDIA GPG key =====
 +
 +<code bash>
 +curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey -o /tmp/nvidia-gpgkey
 +</code>
 +
 +----
 +
 +===== Dearmor the GPG key and save it =====
 +
 +<code bash>
 +gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg /tmp/nvidia-gpgkey
 +</code>
 +
 +----
 +
 +===== Download the NVIDIA container toolkit list file =====
 +
 +<code bash>
 +curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list -o /tmp/nvidia-list
 +</code>
 +
 +----
 +
 +===== Modify the list file to include the signature =====
 +
 +<code bash>
 +sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' /tmp/nvidia-list > /etc/apt/sources.list.d/nvidia-container-toolkit.list
 +</code>
 +
 +----
 +
 +===== Update the package database =====
 +
 +<code bash>
 +apt update
 +</code>
 +
 +----
 +
 +====== Configuring Docker for NVIDIA Support ======
 +
 +Having the **NVIDIA Container Toolkit** in place, the next essential task is configuring Docker to recognize and utilize NVIDIA GPUs.
 +
 +Configure the Docker runtime to use NVIDIA Container Toolkit by using the **nvidia-container-cli** command.
 +
 +  * The Docker configuration file will be modified to use the NVIDIA runtime
 +
 +<code bash>
 +nvidia-container-cli configure --runtime=docker
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  Behind the scenes, this command makes alterations to the **/etc/docker/daemon.json** file.
 +
 +  * As a result, Docker becomes aware of the NVIDIA runtime and can access GPU features.
 +
 +</WRAP>
 +
 +
 +====== Restart the Docker daemon ======
 +
 +<code bash>
 +systemctl restart docker
 +</code>
 +
 +----
 +
 +====== Running the NVIDIA CUDA Docker Image ======
 +
 +With all the required setups in place, the exciting part begins: running a Docker container with NVIDIA GPU support. 
 +
 +NVIDIA maintains a series of CUDA images on Docker Hub.
 +
 +Pull the specific NVIDIA CUDA image:
 +
 +<code bash>
 +docker pull nvidia/cuda:12.2.0-base-ubuntu22.04
 +</code>
 +
 +<WRAP info>
 +**NOTE:** Always check for the latest tags at [[https://hub.docker.com/r/nvidia/cuda/tags?page=1&name=base-ubuntu|NVIDIA CUDA Docker Hub]] to stay updated.
 +
 +  * Use that instead of the 12.2.0-base-ubuntu22.04 tag shown here.
 +
 +</WRAP>
 +
 +----
 +
 +====== Run the Docker container with GPU support ======
 +
 +<code bash>
 +docker run --gpus all -it nvidia/cuda:12.2.0-base-ubuntu22.04 bash
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  This command runs the Docker container with full GPU access (**--gpus all**) and provides an interactive shell inside the container.
 +
 +  * Once inside, use NVIDIA utilities like **nvidia-smi** to confirm GPU access.
 +</WRAP>
 +
 +----
 +
  
docker/gpu/use_nvidia_cuda_toolkit_within_a_docker_container.txt · Last modified: 2025/05/21 16:19 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki