Home > Blockchain >  Create multiple containers of the same service with Docker Compose
Create multiple containers of the same service with Docker Compose

Time:11-29

I am currently starting with Docker Compose, and I wanted to know how to create multiple containers of the same service 'Redis'. I've already tried with docker-compose up --scale redis=3, but it gives an error, I've been searching through Google the possible solution, but I could not find one.

Thank you in advance.

Here is my docker-compose.yml

version: '3'
services:
  redis:
    container_name: redis
    hostname: redis
    image: redis

  redis-commander:
    container_name: redis-dbms
    hostname: redis-commander
    image: rediscommander/redis-commander:latest
    restart: always
    environment:
    - REDIS_HOSTS=local:redis:6379
    ports:
    - "8081:8081"

And here is the error it gives me.

docker-compose up --scale redis=3

Creating network "ex2_default" with the default driver
WARNING: The "redis" service is using the custom container name "redis". Docker requires each container to have a unique name. Remove the custom name to scale the service.
Creating redis-dbms ...
Creating redis      ... done
Creating redis      ...
Creating redis      ...
Creating redis-dbms ... done
ERROR: for redis  Cannot create container for service redis: Conflict. The container name "/redis" is already in use by container "d4c93ae4ca68da0b6430e5eddc657d9dda0f40002c7a81c89368535df05eae24". You have to remove (or rename) that container to be able to reuse that name.

ERROR: for redis  Cannot create container for service redis: Conflict. The container name "/redis" is already in use by container "d4c93ae4ca68da0b6430e5eddc657d9dda0f40002c7a81c89368535df05eae24". You have to remove (or rename) that container to be able to reuse that name.

CodePudding user response:

The error raise because the reason below: Link: https://docs.docker.com/compose/compose-file/compose-file-v3/#container_name

Because Docker container names must be unique, you cannot scale a service beyond 1 container if you have specified a custom name. Attempting to do so results in an error.

You can read more add here: https://github.com/docker/compose/issues/3722

  • Related