【问题标题】:Docker image size for different USER不同用户的 Docker 镜像大小
【发布时间】:2018-02-13 23:43:07
【问题描述】:

更新: 似乎这不是特定于 Docker 的东西,而可能是 Ubuntu 或 useradd 的东西。我发现/var/log/lastlog/var/log/faillog 是罪魁祸首。在做之后:

RUN useradd -ms /usr/bin/tcsh -u 1000 -g users id1000

文件 /var/log/lastlog 的大小约为 288 kB。但是这样做:

RUN useradd -ms /usr/bin/tcsh -u 10000000 -g users id10000000

相反,文件 /var/log/lastlog 的大小为 2.8 GB..

既然我得到了这些新信息,就添加 ubuntu 标记..

这个文件是否会随着添加用户的 UID 而增长?

原帖:

我有以下 Dockerfile(显然不是很有用,为了演示问题而最小化):

FROM ubuntu

RUN apt-get update

WORKDIR /usr/src/

RUN useradd -ms /usr/bin/tcsh -u 1001 -g users daroo ; adduser daroo sudo ; echo daroo:daroo | chpasswd
USER daroo
#RUN useradd -ms /usr/bin/tcsh -u 16777249 -g users otheruser ; adduser otheruser sudo ; echo otheruser:otheruser | chpasswd
#USER otheruser
#RUN useradd -ms /usr/bin/tcsh -u 2000 -g users anotherone ; adduser anotherone sudo ; echo anotherone:anotherone | chpasswd
#USER anotherone

CMD ["/usr/bin/tcsh"]

在底部,我创建了一个用户,将用户添加到 sudo 组,并设置用户的密码(与本演示的用户名相同)。然后我指定 USER 以便在启动容器时从图像中可以看出,该用户帐户是正在运行的容器中使用的用户帐户。

现在的问题:如果我制作三个单独的图像(列出的每个用户一个),对于具有大用户 ID 的用户,图像大小是巨大的.. 对于其他人来说,这是完全合理的.. 什么是什么原因造成的?

% sudo docker build --no-cache -t docker-check-daroo .
[...]
% [edit Dockerfile -> comment RUN and USER lines associated with daroo user, uncomment those for otheruser]
% sudo docker build --no-cache -t docker-check-otheruser .
[...]
% [edit Dockerfile -> comment RUN and USER lines associated with otheruser user, uncomment those for anotherone]
% sudo docker build --no-cache -t docker-check-anotherone .
[...]
% sudo docker images | grep check
docker-check-anotherone         latest                           669db89e8558        4 minutes ago       151MB
docker-check-otheruser          latest                           adcd7cd4906a        5 minutes ago       5.59GB
docker-check-daroo              latest                           e9569a538777        7 minutes ago       150MB

【问题讨论】:

    标签: ubuntu docker size


    【解决方案1】:

    如果其他人遇到此问题,这是我发现的内容和快速解决方法..

    显然,/var/log/lastlog 是一个稀疏文件,它与 Ubuntu 如何确保在创建新用户时不会重复使用 UID 有关。

    似乎可以解决问题的解决方法是在 useradd 命令中简单地包含一个“-l”参数。来自 useradd 手册页:

       -l, --no-log-init
           Do not add the user to the lastlog and faillog databases.
    
           By default, the user's entries in the lastlog and faillog databases are resetted to avoid reusing the entry
           from a previously deleted user.
    
           For the compatibility with previous Debian's useradd, the -O option is also supported.
    

    所以,我刚刚将 Dockerfile 末尾附近的行更新为:

    RUN useradd -ms /usr/bin/tcsh -l -u 16777249 -g users otheruser ; adduser otheruser sudo ; echo otheruser:otheruser | chpasswd
    USER otheruser
    

    无论使用何种 UID,生成的大小都是一致的。

    【讨论】:

    • 谢谢。添加 --no-log-init 解决了我巨大的 dockerfile 问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2022-09-16
    • 2017-05-20
    相关资源
    最近更新 更多