使用 compose 有序构建嵌套 docker 个图像

ordered build of nested docker images with compose

我正在使用 docker-compose 构建一个 lamp。

在我的 docker-compose.yml 中,我有以下内容:

ubuntu-base:
    build: ./ubuntu-base

webserver-base:
    build: ./webserver-base

webserver-base 派生自 ubuntu-base 映像。 在基于网络服务器的 Dockerfile 中:

FROM docker_ubuntu-base

ubuntu-基地建成

FROM ubuntu:14.04

现在,如果我执行 docker-compose.yml,它不会构建 ubuntu-base 映像,但它会尝试构建 webserver-base 映像并失败,因为它找不到 ubuntu-base 图像。

输出:

$ docker-compose up -d
Building webserver-base
Step 1 : FROM docker_ubuntu-base
Pulling repository docker.io/library/docker_ubuntu-base
ERROR: Service 'webserver-base' failed to build: Error: image library/docker_ubuntu-base:latest not found

如果我首先手动构建 ubuntu-base 图像,一切都会起作用。

为什么它不构建 ubuntu-base 映像?

先做一个

docker-compose build ubuntu-base

但这不会在本地创建图像 docker_ubuntu-base,因为您没有任何构建步骤。只会下载 docker.io/ubuntu:14.04。

如果您添加如下构建步骤:

FROM ubuntu:14.04
RUN date

将创建 docker_ubuntu-base 个图像。

所以首先做一个:

docker-compose build ubuntu-base

这将创建图像 docker_ubuntu-base。然后你可以做一个 docker-compose build.

但我建议不要使用这种嵌套的 docker 图像构造。这很麻烦,因为正如@kev 所指出的,您无法控制构建的顺序。为什么不创建两个独立的 docker 文件?让 docker 从 ubuntu-base 派生 webserver-base,方法是尽可能保持 Dockerfile 指令相同并重用层。

遗憾的是,构建排序是 docker-compose 中缺少的功能,现在已经要求了很多个月。

作为解决方法,您可以 link 像这样的容器:

ubuntu-base:
    build: ./ubuntu-base

webserver-base:
    build: ./webserver-base
    links:
      - ubuntu-base

这样 ubuntu-base 在 webserver-base 之前构建。