Docker:多阶段构建会生成多个映像 [英] Docker: Multistage builds result in multiple images

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

问题描述

给出这个多阶段构建的小例子

Given this small example of a multistage build

FROM node:10 AS ui-build
WORKDIR /usr/src/app

FROM node:10 AS server-build
WORKDIR /root/

EXPOSE 3070

ENTRYPOINT ["node"]
CMD ["index.js"]

为什么这会导致我的本地文件系统上出现3张图像?

why does this result in 3 images on my local file system?

"<none>";"<none>";"58d63982fbef";"2020-04-15 17:53:14";"912MB"
"node";"10";"bd83fcefc19d";"2020-04-14 01:32:21";"912MB"
"test";"latest";"3913dd4d03b6";"2020-04-15 17:53:15";"912MB"

我希望有两个映像,即基础映像和服务器构建映像.我使用了标准的docker build命令,即

I expected two images, the base image and the server-build image. I used the standard docker build command, i.e.

docker build -t test . 

那么图像的哪一部分是空的,哪些是测试的?

so which of the parts of the image is none and which is test?

我很困惑

推荐答案

Dockerfile中以 FROM 行开头的每个块都会创建一个新映像.如果使用 docker build -t 选项,则只有最后一个阶段会使用您指定的名称进行标记;否则,将使用您指定的名称进行标记.其余块将在 docker images 输出之类的地方显示为< none> .

Each block in the Dockerfile starting with a FROM line creates a new image. If you use a docker build -t option, only the last stage gets tagged with the name you specify; the remaining blocks will appear as <none> in places like docker images output.

# node:10 is a base image

# Not the final image, will appear as <none>:<none>
FROM node:10 AS ui-build
...

# The final image, will appear as test:latest (`docker build -t` option)
FROM node:10 AS server-build
...

您偶尔会看到Dockerfiles,其中的基础映像在以后的构建阶段中会被重用,并且在那里根本不会显示在 docker images 输出中.

You will occasionally see Dockerfiles where a base image is reused in later build stages, and there it will not show up at all in docker images output.

# Will be hidden because it has descendant images
FROM node:10 AS base
RUN apt-get update && apt-get upgrade

# Will appear as <none>:<none>
FROM base AS ui
...

# Will get the `docker build -t` tag
FROM base

这篇关于Docker:多阶段构建会生成多个映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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