Docker-compose, use multiple containers from the same image

This is what I need to achieve;-

Two, or more, separate docker_compose.yml files, in different directories, where dependencies refer to the same image with a different container name. When I run docker-compose up on one and then the other it should start the images with different container names. But…

In fact what happens is that main app in the docker_compose.yml does start up with a unique name but the dependencies in the second one starting uses the container names from the first and then the master app can’t connect to them because they’re on different networks.

It’s a master app with a dependency on a mysql image. I should get a unique container name for the mysql container. But in fact the second app started tries to use the mysql container started in the first docker-compose up

The container names are generated by environment variables passed down via a startup bash script, e.g.

  container_name: xyz-db_${GIT_BRANCH_NAME}
  image: mysql:5.6.37

The containers, with different names, are derived from same images.

It seems that docker-compose sees that I’m trying to start a container based on image X, then works out that there’s already a container running based on image X and just tries to use that. Which is not what I want it to do

What do I need to do?

You can start as many containers from the same image, as your host is capable to handle.
Though your problem is caused by the “container_name”, which needs to be unique for the docker engine

Why would you even care for the container_name? Servicenames and networks are enough for inter container communication. If it’s about interaction with the docker cli: container labels can be levaraged to identify containers that belong to a docker-compose deployment.