Docker-Compose up picking my last successfully executed container name

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; \
    done;

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