通过扩展
可以扩展或使用多个docker-compose文件及其服务,并在一个文件中将它们连接起来。您可以查看此链接,了解如何使用多个compose文件的其他用法。但是,如果没有像您提到的那样链接相关文件,您无法包含该文件。
这里,我定义了一个common-services.yaml
:
version: '2'
services:
nginx_a:
image: nginx:latest
container_name: nginx
ports:
- 81:80
- 1443:443
然后,我创建了一个docker-compose.yml
文件,并包含了common-services.yml
文件及其自己的服务。
services:
nginx:
extends:
file: common-services.yml
service: nginx_a
nginx_b:
image: nginx:latest
container_name: nginx_b
volumes:
ports:
- 82:80
- 2443:443
.env技术
如果您想避免使用多个文件的链接用法,还有一种使用.env
文件的技术。 我将使用.env
技术重新编写上一个示例。
COMPOSE_PATH_SEPARATOR=:
COMPOSE_FILE=common-services.yml:docker-compose.yml
让我们在common-services.yml中举一个例子,添加另一个服务。
version: '2'
services:
ngin_a:
image: nginx:latest
container_name: nginx_a
ports:
- 81:80
- 1443:443
redis_c:
image: redis:latest
container_name: redis_c
ports:
- 6381:6380
最后,将它们全部加载到docker-compose文件中,甚至不需要提及这些服务。
version: '2'
services:
nginx_b:
image: nginx:latest
container_name: nginx_b
ports:
- 82:80
- 2443:443
env_file:
- .env
最终,您将拥有三个正在运行的服务。