78

I have a Rails application that I want to deploy using Docker on an Ubuntu server. I have the Dockerfile for the application already set up, right now I want to view the nginx conf in its container.

I ran the command below to start an nginx container in an interactive mode:

docker run -i -t nginx:latest /bin/bash

Right now I am trying to install nano editor in order to view the configuration for nginx configuration (nginx.conf) using the commands below:

apt-get update
apt-get install nano
export TERM=xterm

However, when I run the first command apt-get update, I get the error below:

Err:1 http://security.debian.org/debian-security buster/updates InRelease
  Temporary failure resolving 'security.debian.org'
Err:2 http://deb.debian.org/debian buster InRelease                  
  Temporary failure resolving 'deb.debian.org'
Err:3 http://deb.debian.org/debian buster-updates InRelease
  Temporary failure resolving 'deb.debian.org'
Reading package lists... Done    
W: Failed to fetch http://deb.debian.org/debian/dists/buster/InRelease  Temporary failure resolving 'deb.debian.org'
W: Failed to fetch http://security.debian.org/debian-security/dists/buster/updates/InRelease  Temporary failure resolving 'security.debian.org'
W: Failed to fetch http://deb.debian.org/debian/dists/buster-updates/InRelease  Temporary failure resolving 'deb.debian.org'
W: Some index files failed to download. They have been ignored, or old ones used instead.

I have checked very well it has nothing to do with network connectivity. I would need some help. Thank you.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
  • 1
    Edge case (not your issue but I'm including it here): I had the same problem and was running a BIND9 server on the docker host. It allowed LAN resolution (192.168.0.0/24) but containers were connecting from a 172 docker network and so couldn't resolve. Adding 172.0.0.0/8 to the BIND ACL for access solved this. – Dave Mar 21 '22 at 15:52

18 Answers18

127

Try restarting Docker.

One of these should work:

sudo service docker restart
sudo /etc/init.d/docker restart
sudo snap restart docker

Even in cases where Docker never worked before, some commenters said this was helpful. However, you may have a different issue.

Mario Olivio Flores
  • 2,395
  • 1
  • 20
  • 19
  • 1
    Usually restarting the service works for me, but sometimes I need to restart my VM to get it working again. – maja Jul 09 '21 at 08:51
  • 1
    restarting not solving my problem – Harun-Ur-Rashid Jul 15 '21 at 13:36
  • 3
    docker service restart worked for me – Sushil Jun 06 '22 at 14:12
  • 2
    Restarting the service didn't do it for me, but restarting the machine did (Ubuntu 22.04) – web Sep 19 '22 at 11:50
  • Restarting with `sudo snap restart docker` fixed it for me on a fresh docker install, so would recommend trying the restart first, since it's so quick and simple, even if it wasn't "fine in the past". – Nagev Jun 08 '23 at 19:22
96

Specifying a DNS server for docker containers helped me.

Create a /etc/docker/daemon.json file with this content:

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

and restart the docker service:

sudo service docker restart

src: https://docs.docker.com/engine/install/linux-postinstall/#specify-dns-servers-for-docker

Asclepius
  • 57,944
  • 17
  • 167
  • 143
J Kluseczka
  • 1,150
  • 9
  • 14
66

Perhaps the network on the VM is not communicating with the default network created by docker during the build (bridge), so try "host" network :

docker build --network host -t [image_name]

for docker-compose:

service_name:
    container_name: name
    build: 
      context: .
      network: host
Ken Aqshal
  • 23
  • 6
manumazu
  • 783
  • 5
  • 3
29

If you have VPN running, stop it and try again. It solved for me!

Francisco Cardoso
  • 1,438
  • 15
  • 20
8

Here's how I solved it:

Start the docker container for the application in an interactive mode, in my case it an nginx container :

docker run -i -t nginx:latest /bin/bash

Run the command below to grant read permission to the others role for the resolv.conf file:

chmod o+r /etc/resolv.conf

Note: If you are having this issue on your host machine (Ubuntu Linux OS) and not for the Docker containers, then run the same command adding sudo to it in the host machine terminal:

sudo chmod o+r /etc/resolv.conf

Endeavour to exit your bash interactive terminal once you run this:

exit

And then open a new bash interactive terminal and run the commands again:

apt-get update
apt-get install nano
export TERM=xterm

Everything should work fine now.

Reference to this on Digital Ocean: Apt error: Temporary failure resolving 'deb.debian.org'

That's all.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
8
sudo vi /etc/docker/daemon.json

and check flag of iptables, aslo add DNS if not added

{...., "iptables":true,"dns": ["8.8.8.8", "8.8.4.4"]}

then

sudo service docker restart

solved me

Victor Orletchi
  • 469
  • 1
  • 5
  • 15
8

Run this command:

echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" |sudo tee -a /etc/resolv.conf

After that run-

sudo apt-get update

This worked for me.

  • 2
    head --lines 2 /etc/resolv.conf # -> # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN – Richard May 25 '22 at 14:47
5

I had the same problem and in my case it was file access control.

I uses extended acls on the docker root folder and did not realize it, because they where inherited from the folder above (stupid idea to test docker in a "scratch" directory where permissions are set via extended acls).

This lead to the situation that "/etc/resolv.conf" had setting "640" inside the running docker container with a "+" marking the extended acls. But the image did not have extended acls installed and could not handle it.

The weird thing was that, as far as I can see, all other network tools worked (e.g. ping) but only apt could no access the DNS resolver.

After removing the extended acls from the docker root and setting the usual acls, everything worked inside the running container.

Similar to the answer of "Promise Prestion", but solved permanently for new containers, too.

Marco
  • 824
  • 9
  • 13
  • This was a nightmare to debug, but removing the ACLS from the docker data folder solved my issue. This was also the source of many weird errors like https://askubuntu.com/questions/771936/permission-error-when-installing-ttf-mscorefonts-installer-cant-drop-privileges inside the Docker container – skd Mar 15 '23 at 12:26
3

Under Debian, as root, I've ran:

/etc/init.d/docker restart

This solved the issue for me.

Then build and run the container again.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
JemmyX
  • 39
  • 2
2

I easily resolved it via:

- docker exec -it nginx bash (Go inside container)
- ping google.com (if not working)
- exit (Exit from container)
- sudo service docker restart

Please also confirms /etc/sysctl.conf

- net.ipv4.ip_forward = 1

sudo sysctl -p /etc/sysctl.conf

2

I was having the wrong DNS IP address in my /etc/docker/daemon.json. In my case, it was my home router DNS IP address and I was trying from the office network. I found out my office DNS and updated with that.

{ 
  "dns": ["192.168.1.1"] 
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
shubham rasal
  • 91
  • 1
  • 4
  • for me, i also added office dns and it worked. `"dns": ["8.8.8.8","8.8.4.4","192.168.3.1"]` – Faaiz Aug 02 '22 at 09:03
1

I had a similar issue, I tried many suggested solutions, but my issue was gone after I rebooted my VM.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
babis21
  • 1,515
  • 1
  • 16
  • 29
0

Similar issue, under debian. Root cause was a bad DOCKER-USER rule in iptables chain

Those rules have been haded

iptables -I DOCKER-USER -i eno1 -j DROP
iptables -I DOCKER-USER -s 90.62.xxx.xx/32 -i eno1 -j ACCEPT

so removing temporarily following rule fix the point

iptables -D DOCKER-USER -i eno1 -j DROP
c-tools
  • 83
  • 7
0

Coming here from some docker cross compiling headache:

While forking some repo I manually downloaded its root folder containing confd stuff and added it just like the original maintainer did.

ADD root /

after this I was not able to apt update anymore.

I found that the permission of my root named folder was wrong. stat -f "%OLp" root revealed it is 700, but must be 755 to work.

Max
  • 502
  • 2
  • 4
  • 14
0

I'm using Arch version 6.0.11 and docker version 20.10.21, and having this issues inside docker containers.

Thanks to @Marco, that was the initial hint to solve this. The problem is related to the use of extended ACLs in the host system.

The docker root folder has ACLs, you can see this as it has a plus sign at the end of permissions "+":

$ sudo ls -lh /var/lib/docker
drw-rw-r--+    3 root root 4.0K Nov 24  2021 network

And what is the problem? Some docker images does not have ACLs installed, so as it was pointed this causes an issue.

Other network tools like curl resolved DNS properly, but apt or git have problems with DNS resolution.


TLDR; Where is the fix? Modify ACL to set default rx to others.

setfacl -R -d -m o::rx /var/lib/docker

After that, all network tools will be able to perform DNS resolution.

Credits:

Marco comment

Closed git issue in docker

Antoine
  • 1,393
  • 4
  • 20
  • 26
0

The answer from @Mario Olivio Flores suggests using the service command. That is correct and probably works currently on all Linux distributions. However, as most Linux distributions prefer systemd over systemV, it seems better to use just that with the following command:

sudo systemctl restart docker

For example, on my Linux distribution the service script located at /usr/sbin/service just wraps systemctl, see this excerpt:

# When this machine is running systemd, standard service calls are turned into
# systemctl calls.
if [ -n "$is_systemd" ]
then
   UNIT="${SERVICE%.sh}.service"

   case "${ACTION}" in
      restart|status|try-restart)
         exec systemctl $sctl_args ${ACTION} ${UNIT}
      ;;
flyingdutchman
  • 1,197
  • 11
  • 17
0

For me, it turned out to be a random WSL 2 issue -- running wsl --shutdown and restarting the containers solved it for me.

Michał Zawadzki
  • 695
  • 6
  • 14
0

It used to work before, then I got the same error. I've had a Debian and a Docker update in the meantime however. And I've found, that the latest Docker on debian/bookworm has this /etc/docker/daemon.json config:

{
     "bridge": "none"
}

Changing "none" to "" followed by the command systemctl docker restart solved my problem. Seems like it uses the default bridge that I have had before, you may have to create it.

Zoltan Peller
  • 391
  • 4
  • 4