组合多个docker-compose文件

栏目: 编程工具 · 发布时间: 6年前

内容简介:一个服务,一般有开发、集成、测试、生产等多个环境。 在不同环境下,往往又需要不同的配置。 这时,一个参考:以下以一个

一个服务,一般有开发、集成、测试、生产等多个环境。 在不同环境下,往往又需要不同的配置。 这时,一个 docker-compose.yml 文件就难以应付了。 不过,它通过扩展(Extending),对此提供了支持。

Using multiple Compose files enables you to customize a Compose application for different environments or different workflows.

参考: Share Compose configurations between files and projects | Docker Documentation

docker-compose.override.yml

以下以一个 docker-compose.ymldocker-compose.override.yml 为例,展示override的用法。

version: '3'

services:
  nginx:
    image: nginx:stable-alpine
    ports:
      - 127.0.0.1:8080:80
    environment:
      - TEST_0=0
      - DEBUG=true

新建一个 test 目录,把以上内容写入其中。 当只有一个 docker-compose.yml 时,启动后会使用 8080 端口。

$ docker-compose up -d
Creating network "test_default" with the default driver
Creating test_nginx_1 ... done
$ docker-compose ps
    Name             Command          State                    Ports
-------------------------------------------------------------------------------------
test_nginx_1   nginx -g daemon off;   Up      0.0.0.0:8080->80/tcp,0.0.0.0:80->80/tcp
docker-compose exec nginx sh
/ # echo $TEST_0
0
/ # echo $DEBUG
true

如果再增加一个 docker-compose.override.yml ,那么情况会发生变化。

version: '3'

services:
  nginx:
    ports:
      - 80:80
    environment:
      - TEST_1=1
      - DEBUG=false

运行结果如下:

$ docker-compose down
Stopping test_nginx_1 ... done
Removing test_nginx_1 ... done
Removing network test_default
$ docker-compose up -d
Creating network "test_default" with the default driver
Creating test_nginx_1 ... done
$ docker-compose ps
    Name             Command          State                     Ports
---------------------------------------------------------------------------------------
test_nginx_1   nginx -g daemon off;   Up      0.0.0.0:80->80/tcp,127.0.0.1:8080->80/tcp
$ docker-compose exec nginx sh
/ # echo $TEST_0
0
/ # echo $TEST_1
0
/ # echo $DEBUG
false

执行结果是二者的并集。 相同的部分,并非简单的override的关系,而是先后执行。 $DEBUG 变成 false ,不是因为二取一,而是因为 docker-compose.override.yml 后执行,环境变量被第二次赋值所覆盖。 如果对外部的同一个port进行操作,比如 docker-compose.override.yml 也使用 8080 端口,程序就会报错。

$ docker-compose up -d
Creating network "test_default" with the default driver
Creating test_nginx_1 ... error

ERROR: for test_nginx_1  Cannot start service nginx: driver failed programming external connectivity on endpoint test_nginx_1 (c07b1fba8aa720b19fce92053f5b21bf5d8f87816364402032e2401914497601): Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use

ERROR: for nginx  Cannot start service nginx: driver failed programming external connectivity on endpoint test_nginx_1 (c07b1fba8aa720b19fce92053f5b21bf5d8f87816364402032e2401914497601): Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use
ERROR: Encountered errors while bringing up the project.

多个 -f 参数

其实,以上现象,相当于:

docker-compose \
    -f docker-compose.yml \
    -f docker-compose.override.yml \
    up

如果希望 docker-compose.yml 反过来覆盖 docker-compose.override.yml ,可以这样执行:

$ docker-compose -f docker-compose.override.yml -f docker-compose.yml up -d
Creating network "test_default" with the default driver
Creating test_nginx_1 ... done
$ docker-compose exec nginx sh
/ # echo $DEBUG
true

多个 -f 参数,可以指定compose文件的组合顺序,也就可以控制override关系。

总结

需要注意的是,override只对默认的 docker-compose.yml (或 docker-compose.yaml )生效。 docker-compose.yml 作为提交到代码库的东西,有些不方便直接配置的东西, 比如生产环境的密码、生产环境相关的资源配置等,这些都可以通过override的扩展来代为配置。 在不同的环境,可以写不同的 docker-compose.override.yml 。 这样,就可以实现开发、集成、测试、生产等不同环境的不同要求。

除了override以外,同样可以自定义若干YAML文件,甚至不以 docker-compose 为名,利用 -f 进行组合来达成相同目的。 虽然不太方便,但这样更灵活。 即使不使用扩展机制,也可以把所有服务分成多组,放在不同的YAML文件中,实现更灵活的按需启动。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python the Hard Way

Learn Python the Hard Way

Zed Shaw / Example Product Manufacturer / 2011

This is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up t......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具