Activating GPU Access with Docker Compose

gpu access docker compose
24 June 2025


Hello friends! Today we want to discuss activating GPU access in Docker Compose. It is essential to use GPU for processing heavy computations, especially when working with machine learning and deep learning models. Now, let’s see how we can accomplish this using Docker Compose.


First of all, make sure that you have NVIDIA drivers installed on your system and that Docker and Docker Compose are also updated. In fact, to be able to utilize GPU capabilities, it is necessary to use the NVIDIA Container Toolkit. This tool allows Docker to gain access to GPUs.


After installing the drivers and necessary tools, the next step is to configure your Docker Compose file. In this file, you need to use specific configurations for GPU activation. One of these configurations is to use the key `deploy` and the section `resources` to specify the amount required for `reservations` and `limits`.


Now, let’s take a look at a sample docker-compose.yml file that includes these configurations. After reviewing the code, we will go over detailed explanations to completely understand how this works.


version: '3.8'
services:
my_service:
image: my_gpu_image:latest
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
limits:
devices:
- capabilities: [gpu]
runtime: nvidia

Code Explanations



version: '3.8'

The version of Docker Compose that we are using.

services:

The section that defines our various services.

my_service:

The name of our service, which you can change as you like.

image: my_gpu_image:latest

The image that is going to be used. This image must be built previously and should have GPU capabilities.

deploy:

The section that specifies the resources and necessary configurations for running services.

resources:

Defines the resources related to the service.

reservations:

This indicates which type of resources we need.

devices:

Here, you specify the configurations for the devices and their capabilities.

capabilities: [gpu]

This indicates that the specified image must have access to GPUs.

runtime: nvidia

This specifies that for this image, the NVIDIA runtime should be used.

FAQ

?

How can I use GPU in Docker Compose?

?

Can every image utilize GPU?

?

How can I handle GPU-related errors in Docker?

?

Can I use multiple GPUs through Docker Compose?