多个映像,一个Dockerfile [英] Multiple images, one Dockerfile

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

问题描述

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

How to create two images in one Dockerfile, they only copy different files.

这不应该生成两个映像 img1 & img2 ,它会生成两个未命名的图像 d00a6fc336b3 & a88fbba7eede

Shouldn't this produce two images img1 & img2, instead it produces two unnamed images d00a6fc336b3 & a88fbba7eede

Dockerfile:

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-compose 文件,使用 target 选项:

You can use a docker-compose file using the target option:

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

使用您的 Dockerfile 具有以下内容:

using your Dockerfile with the following content:

FROM alpine as img1
COPY file1.txt .

FROM alpine as img2
COPY file2.txt .

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

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