gpt4 book ai didi

docker - 如何防止 docker-compose 多次构建相同的图像?

转载 作者:IT老高 更新时间:2023-10-28 12:41:49 28 4
gpt4 key购买 nike

我的 docker-compose.yml 指定多个图像。其中两个镜像是使用相同的本地 Dockerfile 构建的。它们共享相同的图像名称,但每个都有不同的命令。

在开发过程中,我经常使用 docker-compose up --build 来重建镜像。问题是 docker 两次构建相同的 myimage - 花费的时间比必要的要长。

有没有办法表示镜像只需要构建一次?

version: '2'
services:

abc:
image: myimage
command: abc
build:
context: .
dockerfile: Dockerfile

xyz:
image: myimage
command: xyz
build:
context: .
dockerfile: Dockerfile

最佳答案

根据 the docker-compose file documentation对于构建,为第一个服务指定 build:image:,然后为后续服务仅指定 image:

这是您示例的修改版本,它只构建一次图像(用于 abc 服务)并将该图像重用于 xyz 服务。

version: '2'
services:

abc:
image: myimage
command: abc
build:
context: .
dockerfile: Dockerfile

xyz:
image: myimage
command: xyz

关于docker - 如何防止 docker-compose 多次构建相同的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40899236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com