为什么chown会增加docker映像的大小? [英] Why does chown increase size of docker image?

查看:427
本文介绍了为什么chown会增加docker映像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么'chown'命令应该增加我的docker镜像的大小?



以下Dockerfile创建了一个大小5.3MB的图片:

  FROM alpine:edge 
RUN adduser example -D -h / example -s / bin / sh

此示例创建的图像大小为8.7MB:

  FROM alpine:edge 
RUN adduser示例-D -h / example -s / bin / sh& cho
chown -R example.example / lib

为什么?



注意:我实际的dockerfile当然比该示例更长,因此图像大小的增加也很大。这就是我什至关心的原因。

解决方案

Dockerfile中的每个步骤都会生成一个新的中间映像或层,由与上一层相比,文件系统上发生的任何更改。 docker映像由一组图层组成,这些图层相互叠加以创建最终的文件系统。



如果您有:

  RUN adduser示例-D -h / example -s / bin / sh 

然后,您可能只更改了 / etc / etc / passwd / etc / group 及其影子等效项)。



如果您有:

  RUN adduser example -D -h / example -s / bin / sh&& cho 
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&& b
apk添加-U wget iojs&& b
apk升级&& \
wget -q --no-check-certificate https://ghost.org/zip/ghost-0.6.0.zip -O /tmp/ghost.zip&& \
解压缩-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缓存清理&& \
rm -rf / var / cache / apk / * / tmp / *

我得到:

  $ docker build -t sotest。 
[...]
成功构建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&& b
apk添加-U wget iojs&& b
apk升级&& \
wget -q --no-check-certificate https://ghost.org/zip/ghost-0.6.0.zip -O /tmp/ghost.zip&& \
解压缩-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&& cho
chown -R ghost.ghost *&& \
npm缓存清理&& \
rm -rf / var / cache / apk / * / tmp / *

我得到:

  $ docker build -t sotest。 
[...]
成功构建了696b481c5790
$ docker inspect -f'{{.VirtualSize}}'696b481c5790
31789262

也就是说,两个图像的大小大致相同(
的差约为5 Kb)。



如果 npm install 命令可以成功运行(因为这会安装其他文件),我当然希望生成的图像更大。 p>

I can't understand why the 'chown' command should increase the size of my docker image?

The following Dockerfile creates an image of size 5.3MB:

FROM alpine:edge
RUN adduser example -D -h /example -s /bin/sh

This example however creates an image of size 8.7MB:

FROM alpine:edge
RUN adduser example -D -h /example -s /bin/sh && \
    chown -R example.example /lib

Why?

Note: My actual dockerfile is of course much longer than this example and therefore the increase in image size is also quite larger. That's why I even care..

解决方案

Every step in a Dockerfile generates a new intermediate image, or "layer", consisting of anything that changed on the filesystem from the previous layer. A docker image consists of a collection of layers that are applied one on top of another to create the final filesystem.

If you have:

RUN adduser example -D -h /example -s /bin/sh

Then you are probably changing nothing other than a few files in /etc (/etc/passwd, /etc/group, and their shadow equivalents).

If you have:

RUN adduser example -D -h /example -s /bin/sh && \
    chown -R example.example /lib

Then the list of things that have changed includes, recursively, everything in /lib, which is potentially larger. In fact, in my alpine:edge container, it looks like the contents of /lib is 3.4MB:

/ # du -sh /lib
3.4M    /lib

Which exactly accounts for the change in image size in your example.

UPDATE

Using your actual Dockerfile, with the npm install ... line commented out, I don't see any difference in the final image size whether or not the adduser and chown commands are run. Given:

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/*

I get:

$ docker build -t sotest .
[...]
Successfully built 058d9f41988a
$ docker inspect -f '{{.VirtualSize}}' 058d9f41988a
31783340

Whereas given:

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/*

I get:

$ docker build -t sotest .
[...]
Successfully built 696b481c5790
$ docker inspect -f '{{.VirtualSize}}' 696b481c5790
31789262

That is, the two images are approximately the same size (the difference is around 5 Kb).

I would of course expect the resulting image to be larger if the npm install command could run successfully (because that would install additional files).

这篇关于为什么chown会增加docker映像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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