Docker build “Could not resolve 'archive.ubuntu.com'” apt-get fails to install anything

后端 未结 15 2115
日久生厌
日久生厌 2020-11-28 17:45

I\'ve been trying to run Docker build on various files which previously worked before, which are now no longer working.

As soon as the Docker file included any line

相关标签:
15条回答
  • 2020-11-28 18:13

    In my case, since my containers were in a cloud environment the MTU of the interfaces were not usual 1500 and was like 1450, so I had to configure my docker daemon to set the MTU to 1450 for containers.

    {  
    "mtu": 1454 
    }
    

    look at this : https://mlohr.com/docker-mtu/

    0 讨论(0)
  • 2020-11-28 18:14

    I run into the same problem, but neiter uncommenting /etc/default/docker dns entries nor editing the /etc/resolv.conf in the build container or the /etc/docker/daemon.json helps for me.

    But after I build with the option --network=host the resolving was fine again.

    docker build --network=host -t my-own-ubuntu-like-image .
    

    Maybe this will help someone again.

    0 讨论(0)
  • 2020-11-28 18:14

    I have the same issue, and tried the steps mentioned, but seems none works until refresh the network settings.

    The steps:

    1. As mentioned, add DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true" to /etc/default/docker.
    2. Manually flush the PREROUTING table contents using the iptables -t nat -F POSTROUTING . After running this, restart docker and it will initialize the nat table with the new IP range.
    0 讨论(0)
  • 2020-11-28 18:16

    I found this answer after some Googleing. I'm using Windows, so some of the above answers did not apply to my file system.

    Basically run:

    docker-machine ssh default
    echo "nameserver 8.8.8.8" > /etc/resolv.conf
    

    Which just overwrites the existing nameserver used with 8.8.8.8 I believe. It worked for me!

    Based on some comments, you may have to be root. To do that, issue sudo -i.

    0 讨论(0)
  • 2020-11-28 18:17

    Uncommenting DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" in /etc/default/docker as Matt Carrier suggested did NOT work for me. Nor did putting my corporation's DNS servers in that file. But, there's another way (read on).

    First, let's verify the problem:

    $ docker run --rm busybox nslookup google.com   # takes a long time
    nslookup: can't resolve 'google.com'   # <--- appears after a long time
    Server:    8.8.8.8
    Address 1: 8.8.8.8
    

    If the command appears to hang, but eventually spits out the error "can't resolve 'google.com'", then you have the same problem as me.

    The nslookup command queries the DNS server 8.8.8.8 in order to turn the text address of 'google.com' into an IP address. Ironically, 8.8.8.8 is Google's public DNS server. If nslookup fails, public DNS servers like 8.8.8.8 might be blocked by your company (which I assume is for security reasons).

    You'd think that adding your company's DNS servers to DOCKER_OPTS in /etc/default/docker should do the trick, but for whatever reason, it didn't work for me. I describe what worked for me below.

    SOLUTION:

    On the host (I'm using Ubuntu 16.04), find out the primary and secondary DNS server addresses:

    $ nmcli dev show | grep 'IP4.DNS'
    IP4.DNS[1]:              10.0.0.2
    IP4.DNS[2]:              10.0.0.3
    

    Using these addresses, create a file /etc/docker/daemon.json:

    $ sudo su root
    # cd /etc/docker
    # touch daemon.json
    

    Put this in /etc/docker/daemon.json:

    {                                                                          
        "dns": ["10.0.0.2", "10.0.0.3"]                                                                           
    }     
    

    Exit from root:

    # exit
    

    Now restart docker:

    $ sudo service docker restart
    

    VERIFICATION:

    Now check that adding the /etc/docker/daemon.json file allows you to resolve 'google.com' into an IP address:

    $ docker run --rm busybox nslookup google.com
    Server:    10.0.0.2
    Address 1: 10.0.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
    

    REFERENCES:

    I based my solution on an article by Robin Winslow, who deserves all of the credit for the solution. Thanks, Robin!

    "Fix Docker's networking DNS config." Robin Winslow. Retrieved 2016-11-09. https://robinwinslow.uk/2016/06/23/fix-docker-networking-dns/

    0 讨论(0)
  • 2020-11-28 18:21

    On my system (macOS High Sierra 10.13.6 with Docker 2.1.0.1) this was due to a corporate proxy.

    I solved this by two steps:

    1. Manually configure proxy settings in Preferences>Proxies
    2. Add the same settings to your config.json inside ~/.docker/config.json like:

       "proxies":
      {
        "default":
        {
          "httpProxy": "MYPROXY",
          "httpsProxy": "MYPROXY",
          "noProxy": "MYPROXYWHITELIST"
        }
      }
      
    0 讨论(0)
提交回复
热议问题