【发布时间】:2020-01-17 18:21:23
【问题描述】:
我正在使用 docker,并希望通过在 ubuntu 映像顶部的 zip 文件中安装应用程序来创建一个最小的新层。 ubuntu 镜像既没有安装 wget 也没有安装 unzip,所以我使用 apt-get 安装它们,这需要一个 apt-get update 首先
docker run -it --name app_container ubuntu:latest
# now i have terminal access to the fresh app_container
# install wget and unzip
apt-get update
apt-get install wget -y
apt-get install unzip -y
# download and unpack the app
# ...
# remove wget and unzip
apt-get purge unzip
apt-get purge wget
# i need to remove some dependencies of wget that are no longer needed
apt-get autoremove
此时,如果新 docker 层的唯一内容是新应用程序,那就太好了,但仍然有 apt-get update 创建/更新的文件。我尝试使用删除那些
apt-get clean
不过一个
docker diff app_container
揭示了那里留下了许多更改的文件(不属于应用程序)。好的,所以这些大小可以忽略不计(主要是 /etc/ssl 和 /var/lib/dpkg/info 中的一些残留 wget 证书以及 /var/lib/app/lists 中的 23MB 存档文件),但本着尽可能干净的 docker 层的精神:是否有一种聪明的方法来删除那些也是? ('聪明'的意思是不诉诸蛮力rm)
【问题讨论】:
-
尝试使用
apt-get install --no-install-recommends安装 -
- 最小化 docker 图像链接会导致从 /var/lib/app/lists 中删除所有内容,这似乎是 as-good-as-it-gets。
-
@user2915097 使用
--no-install-recommends的想法会在没有证书验证的情况下安装wget,因此我们必须将wget 与--no-check-certificate选项一起使用。这是可行的,但并不值得,--no-install-recommends似乎修改了与我的初始版本一样多的文件,使用apt get autoremove删除 wget 的依赖项。