'Docker compose command is failing with conflict

I am bringing up my project dependencies using docker-compose. So far this used to work

docker-compose up -d --no-recreate;

However today I tried running the project again after couple of weeks and I was greeted with error message

Creating my-postgres ... error

ERROR: for my-postgres  Cannot create container for service postgres: b'Conflict. The container name "/my-postgres" is already in use by container "dbd06bb1d99eda6f075ea688df16e8b355e559e1759f084dee8f3cddfc535b0b". You have to remove (or rename) that container to be able to reuse that name.'

ERROR: for postgres  Cannot create container for service postgres: b'Conflict. The container name "/my-postgres" is already in use by container "dbd06bb1d99eda6f075ea688df16e8b355e559e1759f084dee8f3cddfc535b0b". You have to remove (or rename) that container to be able to reuse that name.'
ERROR: Encountered errors while bringing up the project.

My docker-compose.yml file is

postgres:
    container_name: my-postgres
    image: postgres:latest
    ports:
      - "15432:5432"

Docker version is

Docker version 19.03.1, build 74b1e89

Docker compose version is

docker-compose version 1.24.1, build 4667896b

Intended behavior of this call is to:

  • make the container if it does not exist
  • start the container if it exists
  • just chill and do nothing if the container is already started


Solution 1:[1]

Docker Compose normally assigns a container name based on its current project name and the name of the services: block. Specifying container_name: explicitly overrides this; but, it means you can’t launch multiple copies of the same Compose file with different project names (from different directories) because the container name you’ve explicitly chosen won’t be used.

You almost never care what the container name is explicitly. It only really matters if you’re trying to use plain docker commands to manipulate Compose-managed containers; it has no impact on inter-service communication. Just delete the container_name: line.

(For similar reasons you can almost always delete hostname: and links: sections if you have them with no practical impact on your overall system.)

Solution 2:[2]

In my case I moved the project in an other directory. When I tryed to run docker-compose up it failed because of some conflicts.

With command docker system prune I resolved them.

Solution 3:[3]

It's caused by being in a different directory than when you last ran docker-compose up. One option is to change back to the original directory. Or if you've configured it as a systemd service you can use systemctl.

Solution 4:[4]

Well...the error message seems pretty straightforward to me...

The container name "/my-postgres" is already in use by container

If you just want to restart where you left, you should use docker-compose start.
Otherwise, just clean up your workspace before running it :

docker-compose down
docker-compose up -d

Solution 5:[5]

Remove --no-recreate flag from your docker-compose command. And execute the command again.

$docker-compose up -d

  • --no-recreate is using for preventing accedental updates.

If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up picks up the changes by stopping and recreating the containers. To prevent Compose from picking up changes, use the --no-recreate flag.

official docker docs.Link

Solution 6:[6]

I had similar issue

dcdown --remove-orphans

That worked for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Maze
Solution 2 70X
Solution 3 Evan
Solution 4 nelsonic
Solution 5 arjundasm
Solution 6 OlaLekanA