Dockerfile中的每个步骤都会生成一个新的中间映像或“层”,该文件由文件系统中与上一层不同的任何内容组成。泊坞窗映像由一组图层组成,这些图层相互叠加以创建最终的文件系统。
如果你有:
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 /lib3.4M /lib
在您的示例中,哪个恰好说明了图像大小的变化。
更新
使用您实际的Dockerfile,并
npm install ...注释掉该行,无论是否运行
adduserand
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}}' 058d9f41988a31783340鉴于:
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}}' 696b481c579031789262即,两个图像的大小大致相同(相差约5 Kb)。
如果
npm install命令可以成功运行,我当然希望生成的图像更大(因为这将安装其他文件)。



