我不明白为什么 'chown' 命令应该增加我的 docker 镜像的大小?
以下 Dockerfile 创建大小为 5.3MB 的镜像:
FROM alpine:edge
RUN adduser example -D -h /example -s /bin/sh
然而,这个例子创建了一个大小为 8.7MB 的图像:
FROM alpine:edge
RUN adduser example -D -h /example -s /bin/sh && \
chown -R example.example /lib
为什么?
注意:我的实际 dockerfile 当然比这个例子长得多,因此图像大小的增加也相当大。这就是为什么我什至关心..
最佳答案
Dockerfile 中的每一步都会生成一个新的中间镜像或“层”,其中包含文件系统上从前一层发生的任何更改。一个 docker 镜像由一系列层组成,这些层被一层一层地应用以创建最终的文件系统。
如果你有:
RUN adduser example -D -h /example -s /bin/sh
那么您可能只更改了 /etc
中的一些文件(/etc/passwd
、/etc/group
和他们的影子等价物)。
如果你有:
RUN adduser example -D -h /example -s /bin/sh && \
chown -R example.example /lib
然后,已更改的内容列表递归地包括 /lib
中的所有内容,这可能更大。事实上,在我的 alpine:edge
容器中,看起来 /lib
的内容是 3.4MB:
/ # du -sh /lib
3.4M /lib
这正是您示例中图像大小变化的原因。
更新
使用您的实际 Dockerfile,注释掉 npm install ...
行,无论 adduser
与否,我都看不到最终图像大小有任何差异和 chown
命令运行。给定:
RUN echo "http://nl.alpinelinux.org/alpine/edge/main" > /etc/apk/repositories && \
echo "http://nl.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
apk add -U wget iojs && \
apk upgrade && \
wget -q --no-check-certificate https://ghost.org/zip/ghost-0.6.0.zip -O /tmp/ghost.zip && \
unzip -q /tmp/ghost.zip -d /ghost && \
cd /ghost && \
# npm install --production && \
sed 's/127.0.0.1/0.0.0.0/' /ghost/config.example.js > /ghost/config.js && \
sed -i 's/"iojs": "~1.2.0"/"iojs": "~1.6.4"/' package.json && \
# adduser ghost -D -h /ghost -s /bin/sh && \
# chown -R ghost.ghost * && \
npm cache clean && \
rm -rf /var/cache/apk/* /tmp/*
我明白了:
$ docker build -t sotest .
[...]
Successfully built 058d9f41988a
$ docker inspect -f '{{.VirtualSize}}' 058d9f41988a
31783340
鉴于:
RUN echo "http://nl.alpinelinux.org/alpine/edge/main" > /etc/apk/repositories && \
echo "http://nl.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
apk add -U wget iojs && \
apk upgrade && \
wget -q --no-check-certificate https://ghost.org/zip/ghost-0.6.0.zip -O /tmp/ghost.zip && \
unzip -q /tmp/ghost.zip -d /ghost && \
cd /ghost && \
# npm install --production && \
sed 's/127.0.0.1/0.0.0.0/' /ghost/config.example.js > /ghost/config.js && \
sed -i 's/"iojs": "~1.2.0"/"iojs": "~1.6.4"/' package.json && \
adduser ghost -D -h /ghost -s /bin/sh && \
chown -R ghost.ghost * && \
npm cache clean && \
rm -rf /var/cache/apk/* /tmp/*
我明白了:
$ docker build -t sotest .
[...]
Successfully built 696b481c5790
$ docker inspect -f '{{.VirtualSize}}' 696b481c5790
31789262
也就是说,两个图像的大小大致相同( 差异约为 5 Kb)。
如果 npm install
命令可以成功运行(因为这会安装其他文件),我当然希望生成的图像更大。
关于docker - 为什么 chown 会增加 docker 图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085621/