stack name with docker-compose

There are several ways to do it:

1. Using --project-name (or -p) option when calling docker-compose:

docker-compose -p "my-app" up

Caution: -p "my-app" must come before up.

2. COMPOSE_PROJECT_NAME environment variable:

export COMPOSE_PROJECT_NAME=my-app
docker-compose up

3. COMPOSE_PROJECT_NAME in .env file

Create a file named .env in the project root and set the COMPOSE_PROJECT_NAME environment variable there:

COMPOSE_PROJECT_NAME=some_app

and then:

docker-compose up

The .env file is read from the folder where the docker-compose command
is executed
, NOT from the folder of the docker-compose.yml file.

Assuming the following project structure:

root
 - database
   - docker-compose.yml
   - .env
 - app
   - docker-compose.yml
   - .env
 - ...

The command below, executed in the root folder, will not give desired effects:

# Stack name 'database' (the folder name). The root/database/.env file not read.
docker-compose -f ./database/docker-compose.yml up

The docker-compose command needs to be executed in the root/database folder:

# Stack name from the root/database/.env
cd database
docker-compose up

If you use option 2 or 3, the project name is applied to all docker-compose commands, as if it were specified with the -p option.

Leave a Comment