Leonardo Guerreiro
Leonardo Guerreiro

Reputation: 1070

Limit container resources on Ubuntu

I'm working on a project that runs a container with 4 processes at once. My computer does not have enough memory and cpu to run all of that. So I found myself in need of limiting the resources each of those processes use. When I run docker stats i get the following output:

docker stats output

As you can see the upper memory limit of all the processes is set to 7.628 Gib which is all the memory in my computer and CPU was low at the time I took the screenshot but it spikes as I use my application.

I am currently using an alias in my bash with the following command to limit the resources of a single container:

docker update --memory-swap=-1 --cpus=2 --memory 4g

So every time I run docker-compose up I have to limit the resources of each container one by one which is really annoying. My question is: is there a way to make it once system-wide so that all the containers I run in my local machine are run with the same resource limits? or maybe, is there a command to apply those limits to all containers at once?

I'm Using Ubuntu 20

I thank any help in advance.

Upvotes: 0

Views: 562

Answers (1)

David Maze
David Maze

Reputation: 158908

Compose file version 2 supports these in the per-service specification.

version: '2.4'
services:
  queue:
    cpu_count: 2
    mem_limit: 4g
    memswap_limit: 4g # -1 doesn't seem to be valid

In general version 2 has more of the docker run options, and version 3 is geared more towards Swarm-specific options. The version 3 equivalent would be the deploy: resources: section, but everything under deploy: is ignored by docker-compose up. Both versions are supported and it's fine to use version 2 Compose files if you need it.

Upvotes: 2

Related Questions