The same docker compose service in multiple folders

Viewed 1922

I have a package (bootstrap) that is included in multiple local projects. Example:

project1/:
    src/...
    tests/...
    vendor/bootstrap/...

project2/:
    src/...
    tests/...
    vendor/bootstrap/...

This package has its internal tests and static code analyzers that I want to run inside each projectX/vendor/bootstrap folder. The tests and analyzers are run from docker containers. I.e. bootstrap has docker-compose.yml with some configuration:

version: '3.7'
services:
  cli:
    build: docker/cli
    working_dir: /app
    volumes:
      - ./:/app
    tty: true

The problem is when I run something inside project1/vendor/bootstrap, then switch to project2/vendor/bootstrap and run something there, docker thinks that I execute containers from project1. I believe it's because of the same folder name as Docker Compose generates container names as [folder-name_service-name]. So when I run docker-compose exec cli sh it checks if there is a running container bootstrap_cli, but it can be created within another bootstrap folder of another project. Example of docker ps:

CONTAINER ID        IMAGE            COMMAND                  CREATED             STATUS                PORTS        NAMES
128c3e834df4        bootstrap_cli    "docker-php-entrypoi…"   55 minutes ago      Up 55 minutes                      bootstrap_cli

NAMES is the same for containers in all these projectX folders.

There is an option to add container_name: bootstrap_project1_cli, but it seems Docker Compose ignores it when searching for a running container.

So is it possible to differentiate containers of the same name and have all of them at the same time?

2 Answers

Have a look at this github issue: https://github.com/docker/compose/issues/2120

There are two options to set the COMPOSE_PROJECT_NAME. Use the -p commandline flag or the COMPOSE_PROJECT_NAME environment variable. Both are documented here: https://docs.docker.com/compose/reference/overview/#compose-project-name

When you run docker-compose, it needs a project name for the containers. If you don't specify the -p option, docker-compose looks for an environment varaible named COMPOSE_PROJECT_NAME. If both are not set, it defaults to the current working directory. Thats the behaviour you have.

If you don't want to add a commandline parameter, you can specify the environment variable in your .env file inside the directory of your docker compose file. See https://docs.docker.com/compose/env-file/

docker-compose is basically just a wrapper around the docker cli. It provides some basic scoping for the services inside a compose file by prefixing the containers (networks and volumes, too) with the COMPOSE_PROJECT_NAME value. If not configured differently, this value corresponds to the directory name of the compose file. You can overwrite this by having according environment variables set. A simple solution would be to place an .env file into the bootstrap directories that contains an instruction like

COMPOSE_PROJECT_NAME=project1_bootstrap

which will lead to auto-generated container names like e.g. project1_bootstrap_cli_1

Details:

Related