docker - 为什么 chown 会增加 docker 图像的大小?

标签 docker filesize chown dockerfile

我不明白为什么 '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/

相关文章:

docker - 带有 docker 的 iptables 阻止传入流量,允许传出流量

java - 无法从docker容器内的spring连接到redis

Android PdfDocument 文件大小

Node.JS:替代 chown 字符串

c++ - 如何在 Linux 中通过 C/C++ 创建另一个用户的文件?

docker - 如何在同一台服务器上托管两个暴露端口 80 的 Docker 容器

docker - 运行.net核心docker镜像,表示应用程序已启动,但在浏览时出现 “This site can’无法达到的错误

binaryfiles - 使用 Extendscript 编写二进制文件。文件大小不正确

ruby - gem ocra 生成巨大文件

linux - Unix 权限 : different chmod on folder and its files