dockerfile 中的 apt-get update 破坏了应用程序的一致性?

apt-get update in a dockerfile destroys app consistency?

在我看过的docker文件中,写docker文件的最佳实践中:https://docs.docker.com/engine/reference/builder/#copy,当apt-get用于安装一些包,apt-get update 总是第一个 运行。我对此有一个担心,因为我们在相应的 docker 容器中构建的应用程序将依赖于这些已安装的软件包,如果最新版本的已安装软件包存在一些不一致,我们构建的软件将无法正常运行更多的。为什么我们不指定包的版本,而是使用 apt-get update

来自 apt-get 的手册页:

update is used to resynchronize the package index files from their sources. The indexes of available packages are fetched from the location(s) specified in /etc/apt/sources.list. For example, when using a Debian archive, this command retrieves and scans the Packages.gz files, so that information about new and updated packages is available. An update should always be performed before an upgrade or dist-upgrade. Please be aware that the overall progress meter will be incorrect as the size of the package files cannot be known in advance.

您可以在 docker 图像上尝试 运行ning apt-get install 而不 运行ning update,但您可能会发现很多东西将无法安装,因为包索引已过时。

更新包数据后,您可以在 运行 install 例如

时为包指定特定版本
apt update && apt install -y \
    git=1:2.7.4-0ubuntu1.4

带有 docker 容器的示例:

> sudo docker run -it ubuntu:16.04 /bin/bash
# root@513eb786d86d:/# apt install git
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package git
root@513eb786d86d:/# apt install git=1:2.7.4-0ubuntu1.4
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package git
root@513eb786d86d:/# apt update
...
root@513eb786d86d:/# apt install git=1:2.7.4-0ubuntu1.4
# works this time!