Docker-Compose up picking my last successfully executed container name

I am using docker-compose in my gitlab pipeline to create containers for each branch. Now everyday I am doing docker system prune -f to clean up the things

When I am executing “docker-compose up for mfbe-80” branch than why in the logs it is showing “Recreating test-for-mj-routingmachine” where " test-for-mj" is the branch which I last executed.


Creating network “cpp-app-routingmachine_default” with the default driver

31 Creating routingmachine-data …

32 Recreating test-for-mj-routingmachine …

By default the project name is the folder name the docker-compose.yml is located in.

What you need to add between docker-compose and up in your command is:

  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)

Deployments using the same project name will result in identical object names (container, networks, volumes…). If your deployments should be distinct, use the --project-name option and provide the branch name as value. Be aware that you need to add it to every command you use to interact with that particular project name.

If you want more flexibility, this post should give you an idea for a more dynamic approach. instead of creating a bash script that exports the environment variables, you can do this in your jenkins pipeline and have it fully dynamic without the need to create an idividual file for each branch.

One more hint. Before redploying a docker-compose stack, you need to make sure to wait until all objects are removed before redeploying.

Somethink like this should do the trick:

    types="service network config secret"; \
    for type in $types; do \
            until [ -z "$(docker $type ls --filter label=com.docker.compose.project=${projetname} -q)" ]; do \
                    sleep 1; \
            done; \

Make sure to populate the environment variable projectname with the appropriate name.

I think my question is not explained clearly. So, what I am telling is I have created a container name called “A” using docker-compose. ( A - my branch name of gitlab repository )

Now I have created another container name “B” using docker-compose. ( B - my branch name of gitlab repository)

I pruned all the images and containers. Next morning when I came I tried to create a new container with name A as my branch than in the logs I can see:

Creating routingmachine-data
Recreating B

and when I do
docker ps
I see container A is there (this is normal as expected)

and when I again try to create container with branch A using docker-compose
than log will be normal like:
Creating routingmachine-data
Recreating A

So basically what I am trying to explain is if I have pruned everything and when I create a new container using docker-compose than why docker automatically show my last executed branch name in the logs but creates the container with actual name
NOTE: I am using CI_COMMIT_REF_NAME variable for branch name with my docker file

uhm, what? I am still not sure what you try to achive.

People leaving the docker playground and getting serious, usualy build their images separately and push it to their private repos, then use the image from the remote repo in their docker-compose.yml. You can make sure that the latest image of the tag is used if your run docker-compose pull before doing up OR run docker-compose up -d --quiet-pull (i could swear in earlier versions the option --pull existed as well)

I am not in your headspace and I am not able to understand your situation yet. I am not sure how A and B are releated to docker-compose and the project-name situation.

How to launch a new docker with the same docker compose file without stopping the ones already launched.

I am using gitlab to create container. Each container is using same compose file but only difference is tag name (which is branch name).

But When I am trying to run the pipeline say “A” and check the container status it is running but when I tried to run pipeline with branch name say “B” than B container is running but “A” container stops automatically.

If I will create a container “C” than status of “C” will be running but “B” will stop

My script in start_container stage is something like this:

DOCKER_COMPOSE_CMD = “docker-compose -f ./docker-compose.common.yml -f ./”

${DOCKER_COMPOSE_CMD} up -d routingmachine-snapshot-run routingmachine-data

Have you ever read my first response?
Did you try to apply what I suggested there?


Thanks @meyay.
It worked

@meyay : Is there any way to override the container name for example in my gitlab pipeline I have my container name routingmachine-data-${CI_COMMIT_REF_NAME}.

But if developer clone the branch than in local dev machine it will fail because ${CI_COMMIT_REF_NAME} environment variable will be not available on local developer machine.

So is there any way I can override the value from
routingmachine-data-${CI_COMMIT_REF_NAME} to routingmachine-data when developer clone the brach

Don’t you already work with multiple compose files to extend the beavior?
What’s wrong with defining multiple “.env” files and overwrite them in your extended files (which in our example would be see:

     - ./ci.env

If you read the lower part of my first response and clicked on the link I provided, you would have seen a draf for a solutation that allows a more elaborate use of variable substitution - it is a simplyfied version of what I personaly use (mine wrapps everything in a Makefile).

I am new in docker. I am reading the docker documentation and implementing the things.
I will try to follow the post which you suggested. Thankyou