【问题标题】:Multiple images, one Dockerfile多个镜像,一个 Dockerfile
【发布时间】:2018-09-20 02:41:06
【问题描述】:

如何在一个 Dockerfile 中创建两个镜像,它们只复制不同的文件。

这不应该产生两个图像 img1img2,而是产生两个未命名的图像 d00a6fc336b3a88fbba7eede

Dockerfile:

FROM alpine as img1
COPY file1.txt .

FROM alpine as img2
COPY file2.txt .

这是 docker build 的结果。

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              d00a6fc336b3        4 seconds ago       4.15 MB
<none>              <none>              a88fbba7eede        5 seconds ago       4.15 MB
alpine              latest              3fd9065eaf02        3 months ago        4.15 MB

【问题讨论】:

标签: docker dockerfile


【解决方案1】:

您可以通过为每个构建阶段运行docker build 从一个 Docker 文件构建多个映像,name 想要从 Dockerfile 中挑选出来 (FROM alpine as name)。

默认情况下,docker 从 Dockerfile 中的最后一个命令构建映像,但是您可以将构建定位到中间层。从问题中的示例 Dockerfile 扩展,可以:

docker build -t image1 --target img1 .

参考:https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target

【讨论】:

    【解决方案2】:

    您可以通过target 选项使用docker-compose 文件:

    version: '3.4'
    services:
      img1:
        build:
          context: .
          target: img1
      img2:
        build:
          context: .
          target: img2
    

    使用您的Dockerfile 包含以下内容:

    FROM alpine as img1
    COPY file1.txt .
    
    FROM alpine as img2
    COPY file2.txt .
    

    【讨论】:

    • 你能在 dockerhub 的自动构建中使用它吗?
    • 我知道这是一个很老的答案,但我目前正在尝试您的解决方案,它向我抛出了一个非常未定义的错误,它说 ERROR: failed to reach build target io.klib.aries.example.x86 in Dockerfile 我使用的代码与您在Dockerfile 和 compose yaml。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2022-12-23
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多