Replicate containers with Docker Compose

·

1 min read

I have always wondered why Docker Compose adds ones at the end of a container name.

image.png

Compose allows you to scale your application by creating replicas of containers. This is controlled by the replicas parameter.

Here is the example:

services:

  queue:
    image: rabbitmq

  worker:
    build: .
    command: python worker.py
    depends_on:
      - queue
    deploy:
      replicas: 3

This will create 3 replicas

image.png

If you want Docker Compose to restart replicas in case of failure, there is a restart_policy parameter.

With this configuration, each replica will be restarted no more than 5 times

services:

  queue:
    image: rabbitmq

  worker:
    build: .
    command: python worker.py
    depends_on:
      - queue
    deploy:
      replicas: 3
      restart_policy:
        max_attempts: 5
        condition: on-failure
        delay: 10s