stack name with docker-compose

风格不统一 提交于 2021-01-02 05:15:27

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/53401672/stack-name-with-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!