In Docker, apt-get install fails with "Failed to fetch http://archive.ubuntu.com/ ... 404 Not Found" errors. Why? How can we get past it?
Solution 1:
You have stated that your Dockerfile contains RUN apt-get -y update
as its own RUN
instruction. However, due to build caching, if all changes to the Dockerfile occur later in the file, when docker build
is run, Docker will reuse the intermediate image created the last time RUN apt-get -y update
executed instead of running the command again, and so any recently-added or -edited apt-get install
lines will be using old data, leading to the errors you've observed.
There are two ways to fix this:
Pass the
--no-cache
option todocker build
, forcing every statement in the Dockerfile to be run every time the image is built.Rewrite the Dockerfile to combine the
apt-get
commands in a singleRUN
instruction:RUN apt-get update && apt-get install foo bar ...
. This way, whenever the list of packages to install is edited,docker build
will be forced to re-execute the entireRUN
instruction and thus rerunapt-get update
before installing.
The Dockerfile best practices page actually has an entire section on apt-get
commands in Dockerfiles. I suggest you read it.
Solution 2:
The issue could be potentially with the ubuntu sources
Check /etc/apt/sources.list
If you see deb http://archive.ubuntu.com/ubuntu main universe restricted multiverse
, that could be the potential issue.
Fix that by replacing it with deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse
Alternatively it could be the mirror itself doesn't respond. archive.ubuntu.com
Name: archive.ubuntu.com
Address: 91.189.88.152
Name: archive.ubuntu.com
Address: 91.189.88.161
Name: archive.ubuntu.com
Address: 91.189.88.149
Replace archive.ubuntu.com with a more trusted mirror say us.archive.ubuntu.com
Name: us.archive.ubuntu.com
Address: 91.189.91.23
Name: us.archive.ubuntu.com
Address: 91.189.91.26
(edit by the oiriginal asker):
Thanks, prateek05! My Dockerfile now starts:
FROM ubuntu:14.04
RUN sed -i'' 's/archive\.ubuntu\.com/us\.archive\.ubuntu\.com/' /etc/apt/sources.list
RUN apt-get -y update
and it seems to be working. But since this is a sporadic issue, only time will tell...
Solution 3:
found something that works!
so i found this: https://www.mail-archive.com/[email protected]/msg5682159.html
The solution is to create a pinning_file
with the contents
# Written by ubuntu-advantage-tools
Package: *
Pin: release o=UbuntuESM, n=trusty
Pin-Priority: never
then add
COPY pinning_file /etc/apt/preferences.d/ubuntu-esm-infra-trusty
to you Dockerfile
before you run the sudo apt-get -y update
Solution 4:
In my case the problem was caused by the parent image since it had not cleared the apt cache properly.
I solve the problem including cleaning commands before the first apt update ...
RUN apt clean && \
rm -rf /var/lib/apt/lists/* && \
apt update && \
...
Hope this helps