如果图像存在,docker compose 可以跳过构建吗? [英] Can docker compose skip build if the image is present?

查看:53
本文介绍了如果图像存在,docker compose 可以跳过构建吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以这样一种方式运行 docker-compose up,使其仅在存储库中不存在图像时才构建?

Is it possible to run docker-compose up in such a way that it will only build if the image isn't present in the repository?

我正在研究一个将在两种不同环境中运行的测试场景:在本地笔记本电脑上,以及作为测试自动化的一部分在服务器上运行.我的 docker-compose 文件看起来像这样:

I'm working on a test scenario that will run in two different environments: locally on a laptop, and on a server as part of test automation. My docker-compose file looks something like this:

services:
  my-service1:
    build: "./local-repo1"
    image: "image1"
  my-service2:
    build: "./local-repo2"
    image: "image2"
  ...

如果我在 local-repo 目录所在的本地运行它,它运行良好.如果我尝试在它从 docker 存储库中提取的服务器上运行它,它会抱怨它找不到构建路径.如果我去掉 build 属性,它在服务器上运行良好,但除非我事先构建图像,否则它不会在本地运行.

If I run it locally where the local-repo directories exist it runs fine. If I try to run it on the server where it instead pulls from a docker repository it complains that it cannot find the build path. If I take out the build property it runs fine on the server, but then it won't run locally unless I build the images beforehand.

有没有办法让它只在图像不存在的情况下尝试构建?如果没有,我可以尝试一些解决方法,但我更愿意只使用一个处理每种情况的 docker-compose 文件.

Is there a way to make it only try to build if the image doesn't already exist? If not I can try some workarounds, but I'd prefer to use just one docker-compose file that handles each case.

推荐答案

您可以使用 docker-compose pull 来获取图像.然后,如果它们已经存在,Compose 将不会尝试再次构建它们.

You can use docker-compose pull to fetch images. Then if they are present already, Compose will not try to build them again.

为确保避免重建,您可以使用 --no-build.

To be really sure to avoid rebuilds, you can use --no-build.

docker-compose pull
docker-compose up -d --no-build

您真正的问题是您正在指定构建上下文,但随后尝试在不存在构建上下文的情况下使用 docker-compose.

Your real problem is that you are specifying a build context, but then trying to use docker-compose without that build context being present.

当 docker-compose 运行时,即使它没有进行构建的计划,它至少会验证构建上下文是否存在.如果没有,那么它就会失败.

When docker-compose runs, even if it has no plan to do a build, it will verify the build context at least exists. If it doesn't, then it will fail.

满足此要求所需要做的就是为任何缺少的构建上下文创建一个空目录.这应该让 docker-compose 运行起来很开心.

All you need to do to satisfy this requirement is create an empty directory for any missing build context. That should make docker-compose happy enough to run.

mkdir -p local-repo1 local-repo2

这篇关于如果图像存在,docker compose 可以跳过构建吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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