skip to Main Content

First I run docker compose up -d command under /redis folder.And a container with ID 9c2223c1cde2 was created.
the compose.yml as fellow:

version: "3.7"
networks:
  wcs-network:
services:
  wcs-redis:
    container_name: wcs-redis
    image: redis:7.0.7
    restart: always
    ports:
      - 6379:6379
    volumes:
      - ./config/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data:/data
    networks:
      - wcs-network
    privileged: true
    command: redis-server /usr/local/etc/redis/redis.conf

When I was trying to refactoring my project, I moved this compose file to /redis/file folder. And changed container network in compose.yml as fellow:

version: "3.7"
networks:
  wcs-network2:
services:
  wcs-redis:
    container_name: wcs-redis
    image: redis:7.0.7
    restart: always
    ports:
      - 6379:6379
    volumes:
      - ./config/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data:/data
    networks:
      - wcs-network2
    privileged: true
    command: redis-server /usr/local/etc/redis/redis.conf

And I get an ERROR:
Error response from daemon: Conflict. The container name "/wcs-redis" is already in use by container "9c2223c1cde2316a19f0db6b40c4eea66fc4d252676c74ed8a847572e58014e7". You have to remove (or rename) that container to be able to reuse that name.

I really want to know how to keep the same container when I change the compose.yml location and it’s content.

2

Answers


  1. first stop all previous containers and remove them. then run new container.

    this error is because there is a container with name /wcs-redis. you can see it with docker ps -a.

    Login or Signup to reply.
  2. Compose has the notion of a project name which has a couple of effects. It is recorded in container metadata and used to construct the Docker-native names of various objects.

    The default project name is constructed from the base name of the current directory. When the Compose file was in /redis the project name defaulted to redis, but now that it is in /redis/file the default project name is file. This means you effectively have two different Compose projects. Since you override container_name: to a fixed string rather than let Compose choose its name, you get a conflict here.

    You can delete the old setup by overriding the project name

    docker-compose -p redis down -v
    docker-compose up -d
    

    (A corollary to this is that you don’t need to include a project-specific prefix in the names in the Compose file itself. You can also let Compose construct its default network, which will be unique per project, choose the container_name:, and choose an image: name for things you build:. This can result in much shorter Compose files than you often see in SO questions.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search