在Docker中,apt-get安装失败,并显示“无法获取http://archive.ubuntu.com/…404未找到”错误 为什么?我们如何才能克服它?
您已声明Dockerfile包含RUN apt-get -yupdate
其自身的RUN
指令。但是,由于构建缓存,如果对Dockerfile的所有更改在文件的后面发生,则在docker build
运行时,Docker将重用上次RUN apt-get -y update
执行的创建的中间映像,而不是再次运行命令,因此所有最近添加或编辑的命令apt-get install
行将使用旧数据,从而导致您观察到的错误。
有两种方法可以解决此问题:
-
将
--no-cache
选项传递给docker build
,强制在每次构建映像时运行Dockerfile中的每个语句。 -
重写Dockerfile以将
apt-get
命令合并为一条RUN
指令:RUN apt-get update && apt-get install foo bar ...
。这样,无论何时编辑要安装的软件包列表,docker build
都将被迫重新执行整个RUN
指令,从而apt-get update
在安装之前重新运行。
该Dockerfile最佳实践页面实际上有上一整节apt- get
的Dockerfiles命令。我建议你阅读。
解决方法
我的团队使用Docker(带有ubuntu:14.04
基本映像)进行本地开发,我们经常不得不重建部分或全部映像。但是apt-get install
,即使在运行后立即下载软件包,我们经常也会失败apt-get -y update
。例如今天我看到
Err http://archive.ubuntu.com/ubuntu/ trusty-security/main libxml2 amd64 2.9.1+dfsg1-3ubuntu4.7
404 Not Found [IP: 91.189.88.161 80]
Err http://archive.ubuntu.com/ubuntu/ trusty-security/main libxml2-dev amd64 2.9.1+dfsg1-3ubuntu4.7
404 Not Found [IP: 91.189.88.161 80]
Fetched 84.7 MB in 1min 6s (1281 kB/s)
Unable to correct missing packages.
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/libx/libxml2/libxml2_2.9.1+dfsg1-3ubuntu4.7_amd64.deb 404 Not Found [IP: 91.189.88.161 80]
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/libx/libxml2/libxml2-dev_2.9.1+dfsg1-3ubuntu4.7_amd64.deb 404 Not Found [IP: 91.189.88.161 80]
E: Aborting install.
显然,特定软件包的特定版本已从存档中删除,并被命名为稍有不同的补丁程序版本取代。例如,正在寻找以上错误,libxml2_2.9.1+dfsg1-3ubuntu4.7_amd64.deb
但是服务器上的版本是libxml2_2.9.1+dfsg1-3ubuntu4.8_amd64.deb
。
通常,可以通过删除基本映像(docker rmi ubuntu:14.04
)并重建来解决此问题;新下载的ubuntu映像具有正确的补丁程序编号,并找到正确的存档文件。但这甚至不总是有效的-
可能是由于新的次要升级到Ubuntu的依赖关系数据库之间的延迟与将该新ubuntu:14.04
映像的部署到Docker Hub 之间存在延迟。
我们已经尝试过使用apt-get
标志--fix-missing
,--fix-broken
而这些标志也不能始终如一地工作。
还有其他想法吗?
apt-get安装失败,并显示“未找到”错误,因为从存储库中删除的软件包是一个类似的问题,但是可接受的答案是不可接受的,因为它不可能自动化。我们的日常开发过程(包括自动构建和部署)全部使用Script编写,并使用Docker,并且每次丢失特定存档时都在Dockerfile中进行黑客入侵是不切实际的(然后在几小时或几天后删除该hack)。
作为对@ prateek05的回应,这是/etc/apt/sources.list
来自官方docker ubuntu:14.04
镜像的:
root@72daa1942714:/# cat /etc/apt/sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://archive.ubuntu.com/ubuntu/ trusty main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates main restricted
## Uncomment the following two lines to add software from the 'universe'
## repository.
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also,please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ trusty universe
deb-src http://archive.ubuntu.com/ubuntu/ trusty universe
deb http://archive.ubuntu.com/ubuntu/ trusty-updates universe
deb-src http://archive.ubuntu.com/ubuntu/ trusty-updates universe
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release,although it includes
## newer versions of some applications which may provide useful features.
## Also,please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ trusty-backports main restricted
deb http://archive.ubuntu.com/ubuntu/ trusty-security main restricted
deb-src http://archive.ubuntu.com/ubuntu/ trusty-security main restricted
deb http://archive.ubuntu.com/ubuntu/ trusty-security universe
deb-src http://archive.ubuntu.com/ubuntu/ trusty-security universe
# deb http://archive.ubuntu.com/ubuntu/ trusty-security multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ trusty-security multiverse