【问题标题】:Docker compose and external images multi-stage buildsDocker 组合和外部图像多阶段构建
【发布时间】:2019-06-09 23:54:33
【问题描述】:

我使用Docker multi-stage build,具体来说:

使用外部图像作为“舞台”

在使用多阶段构建时,您 不限于从您之前创建的阶段复制 Docker 文件。您可以使用 COPY --from 指令从 单独的图像,使用本地图像名称,可用的标签 本地或 Docker 注册表或标签 ID。 Docker 客户端拉取 如有必要,图像并从那里复制工件。语法 是:

就我而言,我有三个 Docker 文件。

一个 Dockerfile 只定义了一个映像,我将其用作构建阶段以在其他两个 Dockerfile 之间共享:

FROM ubuntu:bionic

## do some heavy computation that will result in some files that I need to share in 2 Dockerfiles

并假设我构建了上面的 Dockerfile 给:

docker build -t my-shared-build -f path/to/shared/Dockerfile .

所以现在我有一个在两个容器之间共享的my-shared-build 图像,它们的 dockerfile 看起来像:

FROM ubuntu:bionic

# do something

COPY --from=my-shared-build some/big/folder /some/big/folder

CMD ["/my-process-one"]
FROM ubuntu:bionic

# do something

COPY --from=my-shared-build some/big/folder /some/big/folder

CMD ["/my-process-two"]

我可以构建和运行它们。

回顾一下,我目前所做的是:

1) 构建共享镜像 2) 构建过程一图像 3) 构建进程二镜像

现在我可以运行“进程一”和“进程二”容器。

问题

现在我想使用 Docker Compose 来自动执行“进程一”和“进程二”。

所以问题是:我如何在 Docker Compose 中指定我需要首先构建共享映像并然后构建其他映像(然后运行它们的容器)?

【问题讨论】:

  • 为什么不直接使用 bash 脚本?
  • @Siyu 我认为只要使用 docker compose 就可以实现我想要的......
  • docker-compose 是一个编排工具,而不是构建工具,所以你必须使用技巧来实现你想要的

标签: docker docker-compose dockerfile docker-multi-stage-build


【解决方案1】:

我认为你应该能够做到这一点。

码头工人撰写:

version: '3'
services:
    my-shared-build:
        image: my-shared-build:latest
        build: my-shared-build

    my-process-one:
        image: my-process-one:latest
        build: my-process-one
        depends_on:
            - my-shared-build

    my-process-two:
        image: my-process-two:latest
        build: my-process-two
        depends_on:
            - my-shared-build
            - my-process-one

假设您的 Dockerfile 位于 my-shared-build、my-process-one、my-process-2 子目录中,这应该构建所有 3 个映像(按顺序)

【讨论】:

  • 那么 Docker Compose 怎么知道my-shared-build 不是真正的服务,不应该作为容器启动?
  • 这是一个很好的问题(我没有考虑过)。我可以想到几种方法: docker-compose up --scale my-shared-build=0 或者,创建 2 个 compose 文件(build.yaml 和 docker-compose.yaml) - build.yaml 包含上面提供的文本,而docker-compose.yaml 仅包含 my-process-one 和 my-process-2。 docker-compose -f build.yaml 构建 docker-compose up
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
相关资源
最近更新 更多