Docker撰写如何使用构建扩展服务以使用图像 [英] Docker Compose how to extend service with build to use an image instead

查看:263
本文介绍了Docker撰写如何使用构建扩展服务以使用图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个基础 docker-compose.yml ,如下所示:

Having a base docker-compose.yml like the following:

version: '2'
services:
  web:
    build: .
...

如何将其扩展为使用图像?

How can I extend it to use an image instead?

docker-compose.prod.yml

version: '2'
services:
  web:
    image: username/repo:tag

使用docker运行它 docker-compose -f docker-compose.yml -f docker-compose.prod.yml up 仍然宣传:

Running it with docker docker-compose -f docker-compose.yml -f docker-compose.prod.yml up still promps:


构建网络

步骤1 / x:FROM ...

Building web
Step 1/x : FROM ...

我尝试使用 docker-compose -f docker-compose.yml -f docker-compose.prod.yml up --no-build


错误:需要构建服务'web',但是--no-build已通过。

ERROR: Service 'web' needs to be built, but --no-build was passed.

我期待着从名字/ repo消息中拉出。我有哪些选择?或者我需要创建一个完整的重复文件来处理这种轻微的修改?

I'm expecting the "Pulling from name/repo message" instead. Which options do I have? Or do I need to create a complete duplicate file to handle this slight modification?

推荐答案

省略 build在$ code> docker-compose.yml 的基础上,并将其放在一个 docker-compose.override.yml 文件


当您运行docker-compose时,它会自动读取覆盖。

When you run docker-compose up it reads the overrides automatically.

提取Docker撰写文档

由于您的 docker-compose.yml 文件必须具有构建或映像,我们将使用优先级较低的映像,从而导致:

Since your docker-compose.yml file must have either build or image, we'll use image that has less priority, resulting in:

version: '2'
services:
  web:
    image: repo
[...]

现在,我们转到 docker-compose.override.yml ,默认运行的( docker-compose up docker-compose run web command )。

Now let's move onto docker-compose.override.yml, the one that will run by default (meaning docker-compose up or docker-compose run web command).

默认情况下,我们希望它从我们的 Dockerfile 中构建映像,所以我们可以通过使用 build:。

By default we want it to build the image from our Dockerfile, so we can do this simply by using build: .

version: '2'
services:
  web:
    build: .

生产一个 docker-compose.prod.yml 通过使用 docker-compose -f docker-compose.yml -f docker-compose.prod.yml up 将类似于此一个,除了在这种情况下,我们希望它从Docker存储库中获取图像:

The production one docker-compose.prod.yml run by using docker-compose -f docker-compose.yml -f docker-compose.prod.yml up will be similar to this one, excepting that in this case we want it to take the image from the Docker repository:

version: '2'
services:
  web:
    image: repo

由于我们已经有在我们的基地 docker-compose.yml 文件中,我们可以省略它(但这完全是可选的)相同的 image:repo )。

Since we already have the same image: repo on our base docker-compose.yml file we can omit it here (but that's completely optional).

这篇关于Docker撰写如何使用构建扩展服务以使用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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