简体   繁体   English

覆盖 docker-compose 文件中的图像版本

[英]Overriding image version in docker-compose file

I'm working on overriding default docker-compose.yml file using docker-compose.override.yml as shown in this link , And I can able to specify ports and volumes in the override file.我正在使用docker-compose.override.yml -compose.override.yml 覆盖默认docker-compose.yml文件,如此链接所示,我可以在覆盖文件中指定portsvolumes

In the similar way, is it also possible to specify version of the image which needs to be deployed?同理,是否也可以指定需要部署的镜像版本? If no, what is the best way to handle such circumstance where we need to specify different version for the image?如果不是,处理这种需要为图像指定不同版本的情况的最佳方法是什么?

Any help on this would be great.在这方面的任何帮助都会很棒。

Docker is already having that feature. Docker 已经具备该功能。 I tried to override image name with simple docker-compose, it is working.我试图用简单的 docker-compose 覆盖图像名称,它正在工作。

For example,例如,

docker-compose.yml with content, docker-compose.yml内容,

my-httpd:
  image: httpd:latest
  ports:
    - "1110:80"  

And docker-compose.override.yml with content,docker-compose.override.yml内容,

my-httpd:
  image: httpd:2.4

After the execution of docker-compose -d , here is the docker ps info,执行docker-compose -d后,这里是 docker ps 信息,

在此处输入图像描述

It uses ports from docker-compose.yml (not overrided) and image from docker-compose.override.yml as it is getting overridden here.它使用来自docker-compose.yml (未覆盖)的ports和来自docker-compose.override.ymlimage ,因为它在这里被覆盖。

Note : It you have different names and location, you could use it like the following command instead of docker-compose -d ,注意:如果你有不同的名称和位置,你可以像下面的命令一样使用它而不是docker-compose -d

docker-compose -f <DOCKER COMPOSE LOCATION> -f <OVERRIDE FILE LOCATION> up -d

Edit 2:编辑 2:

Overriding in the above manner will only replace the non array values and the array variables will be merged.上述方式覆盖只会替换非数组值,数组变量会被合并。

For example, if I have ports in both files which is an array, it will bind both the ports instead of taking ports value just from the overriding file unlike the image part (non array).例如,如果我在两个文件中都有ports ,这是一个数组,它将绑定两个端口,而不是像image部分(非数组)那样仅从覆盖文件中获取端口值。

There is (now?) a better option for what you want to do.有(现在?)你想做的事情有更好的选择。 From https://docs.docker.com/compose/environment-variables/来自https://docs.docker.com/compose/environment-variables/

It's possible to use environment variables in your shell to populate values inside a Compose file:可以在 shell 中使用环境变量来填充 Compose 文件中的值:

web:
  image: "webapp:${TAG}"

For example you need to have 3 versions of docker:例如你需要有 3 个版本的 docker:

  • docker-composer-master.yml docker-composer-master.yml

  • docker-composer-dev.yml docker-composer-dev.yml

  • docker-composer-docker.yml docker-composer-docker.yml

You can create 3 file docker-composer and add content as the below picture您可以创建 3 个文件 docker-composer 并添加内容如下图

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM