docker - is a running container created by each RUN command during multistage build?
问题描述
I'm new to Docker, sorry if my question sounds dumb. below is a dockerfile from a textbook:
FROM diamol/base AS build-stage
RUN echo 'Building...' > /build.txt #line 2
FROM diamol/base AS test-stage
COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' >> /build.txt #line 6
FROM diamol/base
COPY --from=test-stage /build.txt /build.txt
CMD cat /build.txt
and the author says:
The RUN instruction executes a command inside a container during the build, and any output from that command is saved in the image layer.
My questions are:
Q1-Since the author mentioned RUN instruction executes a command inside a container during the build, does it mean that in the first RUN command in line 2, a contianer is created based on the diamol/base
image, and this container modify the content of the files then commit the change back to diamol/base
image? and same thing happen in line 6, is my understanding correct?
Q2-If my understanding is correct, isn't that we have three different diamol/base images- the original one and two modified in build-stage and test-stage, isn't it very inefficient because it just a small file(let's say its size is 3kb) inside is differnt, while the image could be as large as 500MB and we have 3 copies which is 500MB*3 = 1.5G?
解决方案
Q1-The base image is unchanged with FROM diamol/base
, you restart from the original image each time. The previous image is used by --from=build-stage
. For each step in your build, there is a layer created which can be reused to speed up later build.
Q2-Images are composed of layers, the layers are reused between images. There is maybe a bit of overhead but the space is not duplicated. Images are immutable, the container based on the image contains the changes.
A Docker image is built up from a series of layers. Each layer represents an instruction in the image’s Dockerfile. Each layer except the very last one is read-only.
推荐阅读
- javascript - nginx和奇偶校验的Websocket连接?
- excel - 在大型 Excel 文档中查找数据以便稍后与找到的其他数据一起使用的参数
- r - 允许引用具有索引和名称的列的函数
- jenkins - Jenkins 代理作为 Jenkins 管道中阶段级别的变量
- python - python:包含对 lambda 函数的引用的导入全局变量的状态
- sql - 删除列名中带引号的列
- node.js - values 必须是一个以值名作为键的对象
- sql-server - 有关 SQL Server 2016 SP1 上 AlwaysOn 复制的问题
- javascript - 为什么状态没有被解析为 nextjs 中的变量
- oracle-apex - 会话过期和 PC 从睡眠模式唤醒时如何重定向到其他页面 - Oracle Apex