How to Use Graphics Cards with Docker Containers

How to Use Graphics Cards with Docker Containers

Using graphics cards with Docker containers can greatly enhance the performance of applications that require GPU resources, such as machine learning, deep learning, and complex computations. In this article, we will explore how to effectively utilize graphics cards with Docker containers.

Prerequisites for Using Graphics Cards with Docker

Before you start, ensure you have the following:

  • A compatible NVIDIA GPU installed on your host machine.
  • Docker installed and running on your system.
  • nvidia-docker2 package to enable GPU support in Docker.

Installing NVIDIA Drivers

To use the GPU with Docker containers, you need to install NVIDIA drivers. Follow these steps:

  1. Update your package list:
  2. sudo apt-get update
  3. Install the appropriate NVIDIA driver. For instance:
  4. sudo apt-get install nvidia-driver-
  5. Reboot your system to apply changes:
  6. sudo reboot

Installing NVIDIA Container Toolkit

The NVIDIA Container Toolkit allows Docker to utilize the GPU. Here’s how to install it:

  1. Set up the package management repository:
  2. distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
    curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
    curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
  3. Install the NVIDIA docker runtime:
  4. sudo apt-get update
    sudo apt-get install -y nvidia-docker2
  5. Restart the Docker service:
  6. sudo systemctl restart docker

Running a Docker Container with GPU Support

Now that you have set everything up, running a Docker container with GPU access is straightforward. Use the following command to run a container:

docker run --gpus all -it --rm nvidia/cuda:11.0-base sh

In the above command:

  • --gpus all allocates all available GPUs to the container.
  • -it makes the container interactive, allowing you to access a shell.
  • --rm automatically removes the container after exit.

Verifying GPU Access Inside the Container

To ensure that the Docker container can access the GPU, you can run the following command inside the container:

nvidia-smi

This command will display the GPU status and confirm that the container is using the NVIDIA GPU.

Using Docker Compose with GPU Support

If you are using Docker Compose, you can specify GPU support in your docker-compose.yml file:

version: '3.8'
services:
  my_service:
    image: nvidia/cuda:11.0-base
    runtime: nvidia
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all

Conclusion

Using graphics cards with Docker containers allows for efficient execution of resource-intensive applications. By following the steps outlined, you can harness the power of your NVIDIA GPUs in a containerized environment, enabling better performance for applications in data science, artificial intelligence, and gaming.

As the technology evolves, always stay updated with the NVIDIA and Docker documentation to utilize the latest features and best practices for GPU computing.