I was configuring Apache server using an ubuntu:latest image and during the process, i encountered an unusual error where i couldn't apt-get anything at all on my Ubuntu docker machine. I get the error as seen in the screen shot below.

Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease Temporary failure resolving ‘archive.ubuntu.com’

After three days of searching and doing a lot of googling i happen to have a breakthrough with this problem and i hope this helps anyone out there with this problem and save you valuable 72 hours.

At this point you probably have restarted your docker engine. maybe deleted and installed again and still nothing is working. Remember we have to first understand a problem before we even take a path to resolving it.

Here is a snippet of my dockerfile

FROM ubuntu:18.04MAINTAINER Faithful <faithful@infused.io>RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_RUN www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

So when it gets to step 3 which is

RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*

Reason for this problem :

Sometimes the network connection for docker could be faulty and can lead to weird errors because of a couple of reasons One quick way out of this is to first troubleshoot and see if DNS lookups are failing in the docker images.

Check if DNS is the problem :

Check if basic internet connection is working by pinging a public ip address lets say nslookup google.com by running the command below

$ docker run busybox nslookup google.com# we should get a reply like this 
Server: 8.8.8.8
Address 1: 8.8.8.8
nslookup: can't resolve 'google.com'

If you get something like the image below, it obviously means it is failing to resolve DNS

Containers use google dns server 8.8.8.8 to resolve DNS. unless you have a dns server locally defined in your resolv.conf file(usually /etc/resolv.coff). Containers wont reach the internet if public dns is blocked. This issue will also arise in such case.

To fix this?

I recommend the quick fix if you are temporarily in a restrictive network and you need to run containers directly.

Although, most of the time you might want this to work on your environment and for any other problem that is dependent on docker. For that please use the permanent fix.

We need to use a custom DNS server (which overrides Docker’s DNS) to run a docker container. it's quite easy to directly run a docker container with a custom DNS server.

Let's first get the address of our DNS server. Run this command in your terminal

$ nmcli dev show | grep 'IP4.DNS'
IP4.DNS[1]: 192.10.0.2

To run a docker container with this DNS server, provide the --dns flag to the run command. For example, let’s run the command we used to check if DNS is working:

$ docker run --dns 192.10.0.2 busybox nslookup google.com
Server: 192.10.0.2
Address 1: 192.10.0.2
Name: google.com
Address 1: 2a00:1450:4009:811::200e lhr26s02-in-x200e.1e100.net
Address 2: 216.58.198.174 lhr25s10-in-f14.1e100.net

Sweet as!

On your root folder where you have your Dockerfile you can run this command which tells docker to use the same network as your local one docker build --network=host -t my-image-name .

System-wide fix

Update docker daemon

you need to change the DNS settings of the Docker daemon. You can set the default options for the docker daemon by creating a daemon configuration file at /etc/docker/daemon.json

You should create this file with the following contents to two servers, firstly your network’s DNS server, and secondly the Google DNS server to fall back to in case that server isn’t available:

/etc/docker/daemon.json:

{
"dns": ["192.10.0.2", "8.8.8.8"]
}

Then restart the docker service:

sudo service docker restart

Testing the fix

Now you should be able to ping google.com successfully from any Docker container without explicitly overriding the DNS server e.g:

$ docker run busybox nslookup google.com
Server: 192.10.0.2
Address 1: 192.10.0.2
Name: google.com
Address 1: 2a00:1450:4009:811::200e lhr26s02-in-x200e.1e100.net
Address 2: 216.58.198.174 lhr25s10-in-f14.1e100.net

Bonus

To run assign/map a port from your host to the exposed container port

docker run -p 3000:8080 -d image name

All thanks of course goes to the amazing peeps that save our ass every day in stack overflow Q&A. See the link below.

https://stackoverflow.com/questions/24991136/docker-build-could-not-resolve-archive-ubuntu-com-apt-get-fails-to-install-a

--

--