docker-compose scale with different cpuset

2.3k Views Asked by At

How can I scale a service but apply a different cpuset on each instance with docker-compose ?

For example: I have 4 cpus, I want 4 instances, each using 1 unique cpu.

1

There are 1 best solutions below

3
On

What version of docker-compose are you using? I'm asking because accomplish what you desire is only possible with docker-compose v2.x or docker-swarm as you can see below.

enter image description here

you can check more info here in the docker doc.

supposing that you are using docker-compose 2.4, you can define a service like this in your `docker-compose.yaml

version: '2.4'

services:
  redis:
    image: redis:1.0.0
    restart: always
    environment:
      - REDIS_PASSWORD=1234
    cpu_count: 1
    mem_limit: 200m

Where cpu_count is number o cpu cores you want to use in the service, and mem_limit is the limit of memory that your service can consume.

To define the number of replicas you must run: docker-compose up --scale redis=2

Where redis is name of the service in the docker-compose and 2 is the number of replicas that you desire. So both the two containers will spin up with 1 core of CPU and 200m of memory.

To check the container resources consumption you can run docker stats

Source: https://docs.docker.com/engine/reference/run/#runtime-constraints-on-resources

https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources