Reading time: 3 minutes
In Docker Compose, you can limit the RAM, disk space, and CPU usage of containers to prevent them from consuming excessive resources on the system.
Here’s a tutorial on limiting RAM, disk space, and CPU usage in Docker Compose containers.
Step 1: Create the docker-compose.yml file
First, you need to create the docker-compose.yml file in the root directory of your project. This file defines the services and configurations for your containers.
Step 2: Define the services
In the docker-compose.yml file, define the services you need for your project. For example:
version: '3' services: app: build: . image: my-app container_name: my-app restart: always
In this example, a service named app
is defined.
This service will be built from the Dockerfile located in the current directory (.
), tagged with the image name my-app
, and always restarted on failure.
Step 3: Limit RAM
To limit the RAM of a container, use the mem_limit
option. For example:
version: '3' services: app: build: . image: my-app container_name: my-app restart: always mem_limit: 1g
In this example, the container’s RAM is limited to 1 GB.
Step 4: Limit disk space
To limit the disk space of a container, use the storage_opt
option. For example:
version: '3' services: app: build: . image: my-app container_name: my-app restart: always mem_limit: 1g storage_opt: size: 1g
In this example, the container’s disk space is limited to 1 GB.
Step 5: Limit CPU usage
To limit the CPU usage of a container, use the cpus
option. For example:
version: '3' services: app: build: . image: my-app container_name: my-app restart: always mem_limit: 1g storage_opt: size: 1g cpus: 0.5
In this example, the container’s CPU usage is limited to 50%.
Step 6: Run the containers
To run the containers with the defined limitations, use the following command:
docker-compose up -d
This command starts all the services defined in the docker-compose.yml file and runs them in the background (-d
This command starts all the services defined in the docker-compose.yml file and runs them in the background (-d
).
With these steps, you can limit the RAM, disk space, and CPU usage of your Docker Compose containers.
Step 7: Monitor resource usage
You can view the resource usage of Docker Compose using the docker stats
command. This command provides real-time statistics for all running containers, including CPU, memory, and I/O usage.
To use docker stats
with Docker Compose, simply run the command in the terminal:
docker stats
This will display the statistics for all running containers in the system. If you want to limit the output to a specific set of containers, you can specify the container names as arguments to the docker stats
command.
For example, if you have a service named “app” in your docker-compose.yml file, you can view the statistics only for that service with the following command:
docker stats app
You can also use the --format
option to specify the output format. For example, to display only the container name and memory usage, you can use the following command:
docker stats --format "table {{.Name}}\t{{.MemUsage}}"
This will display a table with the container name and memory usage for each running container.
In summary, you can use the docker stats
command to view real-time statistics of running containers, and you can limit the output to a specific set of containers using the container name arguments. You can also use the --format
option to customize the output format.