在另一个 compose 文件中导入 docker compose 文件 [英] Import docker compose file in another compose file

查看:27
本文介绍了在另一个 compose 文件中导入 docker compose 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以导入"?或者将一个 docker-compose 文件链接到另一个 docker-compose 文件?

Is it possible to "import" or link a docker-compose file into another docker-compose file?

假设我有两个文件:

# docker-compose-1.yml
services:
    A:
        # config
    B:
        # config

# docker-compose-2.yml
services:
    C:
        # config
    import: docker-compose-1.yml

通过运行 docker-compose -f docker-compose-2.yml up,我想启动容器 A、B(在导入的文件中指定)和 C.这可能吗,没有必须使用 -f 参数链接两个文件?

By running docker-compose -f docker-compose-2.yml up, I would like to start containers A, B (specified in the imported file) and C. Is this possible, without having to link both files with the -f parameter?

推荐答案

通过扩展

可以扩展或使用多个 docker-compose 文件及其服务,并将它们链接到一个文件中.您可以查看此链接以了解多个撰写文件的其他用法.但是,如果不按照您提到的将相关文件链接在一起,您还不能包含该文件.

It's possible to extend or use multiple docker-compose files and their services and link them in just one file. You can take a look at this link to understand how is the other usages of multiple compose files. But you can't include the file yet without linking related files together as you mentioned.

这里,我定义了一个 common-services.yaml:

Here, I defined a 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 文件和它自己的服务.

And then, I created a docker-compose.yml and included the common-services.yml file and its own service as well.

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 技术重写前面的示例.

And if you want to avoid chaining usage of multiple files, there is also a technique with .env files. I will rewrite the previous example with .env technique.

COMPOSE_PATH_SEPARATOR=:
COMPOSE_FILE=common-services.yml:docker-compose.yml

让我们在 common-services.yml 中添加另一个服务作为示例

Let's add another service as an example in the 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 文件中,甚至不提及这些服务.

And finally, load all of them in the docker-compose file without even mention to those services.

version: '2'
 services:
   nginx_b:
     image: nginx:latest
     container_name: nginx_b
     ports:
       - 82:80
       - 2443:443
     env_file:
       - .env

最后,您将拥有三个正在运行的服务.

In the end, you will have three running services.

这篇关于在另一个 compose 文件中导入 docker compose 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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