带有docker-compose的堆栈名称 [英] stack name with docker-compose

查看:75
本文介绍了带有docker-compose的堆栈名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用docker-compose时是否可以设置堆栈名称?

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.调用 docker-compose :

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

docker-compose -p"my-app";上

警告: -p"my-app" 必须在 up 之前.

2.COMPOSE_PROJECT_NAME环境变量:

export COMPOSE_PROJECT_NAME=my-app
docker-compose up

3..env文件中的COMPOSE_PROJECT_NAME

在项目根目录中创建一个名为 .env 的文件,并在其中设置COMPOSE_PROJECT_NAME环境变量:

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

COMPOSE_PROJECT_NAME=some_app

然后:

docker-compose up

.env 文件是从docker-compose命令所在的文件夹中读取的,而不是从docker-compose.yml文件的文件夹中执行.

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.

假定以下项目结构:

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

docker-compose命令需要在根目录/数据库文件夹中执行:

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

如果使用选项2或3,则将项目名称应用于所有docker-compose命令,就像使用 -p 选项指定的一样.

这篇关于带有docker-compose的堆栈名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆