Docker apt-get update fails

后端 未结 8 1263
天命终不由人
天命终不由人 2020-12-23 16:47

Can somebody help me get apt-get working in my docker container? Whenever I try running any apt-get command in my docker container, the command fails. I\'m running Docker ve

8条回答
  •  离开以前
    2020-12-23 17:24

    $ sudo docker run -i -t --net=host ubuntu /bin/bash
    # apt-get update
    

    No errors! - Note the added --net=host.


    Below I show how to reproduce the error.
    I did the following on a clean machine having nothing but Ubuntu 18.04 installed on it.

    First install docker. Copy-paste one line at a time and hit Enter after each line:

    sudo apt update                         # took 11 seconds for me
    sudo apt install -y apt-transport-https ca-certificates \
    curl software-properties-common
    

    The second of the two commands above (one command broken up on two lines) is optional and took ~20 seconds for me.

    echo "cacert=/etc/ssl/certs/ca-certificates.crt" >> ~/.curlrc
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
    | sudo apt-key add -
    
    sudo add-apt-repository "deb [arch=amd64] https://\
    download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    

    The command above took ~7 seconds for me.

    sudo apt update                         # ~3 seconds this time
    sudo apt install -y docker-ce           # ~1 minute for me
    

    Optional -- if you don't like to prefix docker with sudo every time:

    sudo usermod -aG docker ${USER}
    

    Log out and back in.
    Confirm that docker is now added to your groups:

    id -nG
    

    Now to the core part of interest:

    $ sudo docker run -it ubuntu /bin/bash  # ~10 seconds for me
    # apt update                            # first Err after ~1 minute
    Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
      Connection failed [IP: 91.189.88.152 80]
    Err:2 http://security.ubuntu.com/ubuntu focal-security InRelease
      Connection failed [IP: 91.189.88.152 80]
    Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
      Connection failed [IP: 91.189.88.152 80]
    Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
      Connection failed [IP: 91.189.88.152 80]
    ...
    W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease [...]
    ...
    W: Some index files failed to download. They have been ignored, [...]
    

    Note how I just recreated the error described by the OP!
    The OP got Could not resolve 'archive.ubuntu.com',
    while I got Failed to fetch http://archive.ubuntu.com.
    But the root cause is the same: the docker container has no access to the internet.

    I don't want to stay in this bad docker container so I hit Ctrl + D.
    No wish to keep it either, so I do:

    sudo docker container ps -a
    

    which tells me that the CONTAINER ID in my case is c05ec98236f0.

    I remove this container:

    sudo docker container rm c05ec98236f0
    

    Heads up for something that doesn't give me an error.

    $ sudo docker run -it --net=host ubuntu bash
    # apt update                            # ~7 seconds
    Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
    ...
    Fetched 16.4 MB in 4s (4332 kB/s)
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    3 packages can be upgraded. Run 'apt list --upgradable' to see them.
    

    Voilà! No error!

    References:
    https://askubuntu.com/questions/1162163#1162198
    https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04
    https://stackoverflow.com/questions/3160909#31424970
    https://stackoverflow.com/questions/43316376#43317607

提交回复
热议问题