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

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

问题描述

是否有可能导入还是将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技术

如果您要避免链接多个f iles,还有一种使用.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.

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

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