Make:对Docker映像的依赖 [英] Make: dependency on a docker image

查看:41
本文介绍了Make:对Docker映像的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker工作流基于给定的spec文件生成一些文件,其中Makefile是(它根据OpenAPI规范生成客户端):

I'm using a docker workflow to generate some files, based on a given spec file, with the Makefile being (it's generating a client according to an OpenAPI spec):

SWAGGER ?= ${PWD}/swagger.yaml
GENERATOR ?= openapitools/openapi-generator-cli\:latest

generated: Makefile ${SWAGGER}
    docker run --rm --user $$(id -u):$$(id -g) \
    -v ${PWD}:/output -v ${SWAGGER}:/input/swagger.yaml \
    ${GENERATOR} \
    generate -g python -i /input/swagger.yaml -o /output/generated \

这很好,如果我修改输入的SPEC文件,它将重新生成.

this works fine, and will rebuild if I modify the input SPEC file.

但是当更改docker映像时,它不会重建.

But it doesn't rebuild when the docker image is changed.

假设我再次 docker build 具有相同 name:tag 的图像,但是内部具有不同的代码,或者我使用上游图像的不同标记版本,任何.这是预料之中的,因为Makefile不知道Docker映像的内容或修改日期.如何使Makefile理解对docker映像的依赖性?

Let's say I docker build the image with the same name:tag again, but with different code inside, or I use a different tagged version of the upstream image, whatever. This is kind of expected because the Makefile has no knowledge of the docker image's content or modification date. How can I make the Makefile understand the dependency on the docker image ?

  • 我试图 docker检查图像以获取创建日期,但是我不知道如何使 make 理解为依赖项(如果创建日期比输出目录更新,然后重新构建)
  • 我不能只添加对docker映像中代码的依赖,因为docker映像甚至可能不是由本地可用文件构建的.
  • I've tried to docker inspect the image to fetch the creation date, but I don't know how to make make understand that as a dependency (if the creation date is newer than the output dir, then rebuild)
  • I can't just add a dependency on the code inside the docker image, because the docker image might not even have been built from locally available files.

make 可能不是用于这种事情的工具,也许我可以使用其他一些东西来了解docker映像的依赖性.

make might not be the tool for that kind of thing, maybe there is something else that I could use that understands the docker image dependency.

推荐答案

您所依赖的工件是Docker映像还是其他位置的某些服务/数据,您需要表示您了解的 格式为常规文件.因为 make 仅处理文件.:-)

Whether the artifact you depend on is a Docker image or some service/data somewhere else, you need to represent what you know about it, in the form of a regular file. Because make only deals with files. :-)

我建议您为"调用 docker build 定义一些宏,并将其成功的证据保存到具有可预测名称的标记文件中" ,以便您可以使用它替换对 docker build 的所有调用,确保文件处理的一致性.完整的示例,假设存在 a/Dockerfile b/Dockerfile .

I'd recommend you define some macro for "invoke docker build and save evidence of its success to a marker file with predictable name" so that you can use that to replace all calls to docker build, ensure consistent file handling. Full-blown example, assuming a/Dockerfile and b/Dockerfile exist.

# For consistency, the src dirs are named like the images they produce
IMAGES = a b

# Keep "stamps" around, recording that images were built. 
# You could keep them in e.g. a `.docker-buildstamps/*` dir, 
# but this example uses `*/docker-buildstamp`.
BUILDSTAMP_FILE = docker-buildstamp
BUILDSTAMPS = $(addsuffix /$(BUILDSTAMP_FILE),$(IMAGES))

.PHONY: all
all: $(BUILDSTAMPS)

# Pattern rule: let e.g. `a/docker-buildstamp` depend on changes to `a/*` (-but avoid circular dep)
%/$(BUILDSTAMP_FILE): % %/[!$(BUILDSTAMP_FILE)]*
    $(docker_build)

clean:
    docker image rm -f $(IMAGES)
    rm -f $(BUILDSTAMPS)

# Turn `a/docker-buildstamp` back into `a`
define from_buildstamp
$(@:%/$(BUILDSTAMP_FILE)=%)
endef

# Self-explanatory
define docker_build
docker build -t $(from_buildstamp) $(from_buildstamp) 
touch $@
endef

这篇关于Make:对Docker映像的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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