Answer a question

Is there a way to set the stack name when using docker-compose?

Currently it takes the folder name (which is a horrible area) and that leads to some confusion.

For example, we have several projects that have a database folder that contains the database stack. When running these on a single host, we have now several database stacks.

Answers

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.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐