1

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.

0xkvn
  • 377
  • 2
  • 16

1 Answers1

0

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

Daniel Marques
  • 1,249
  • 8
  • 17
  • Thanks for your answer! I'm using docker-compose v3.8. I'm not familiar with swarm yet so I might need to task a deeper look at it. – 0xkvn Sep 10 '20 at 14:52
  • 1
    After reading the long github thread (https://github.com/docker/compose/issues/4513) about the removal of non-swarm config from v3 docker-compose, it turns out that v2 is supposed to be used for non-swarm usage (which doesn't really makes sense but whatever...). Do you have a solution to my first issue now that I'm using docker-compose v2.4 ? – 0xkvn Sep 10 '20 at 17:04
  • 1
    I edited my answer, I hope it can be helpful to you. – Daniel Marques Sep 11 '20 at 15:59