docker容器W: Some index files failed to download. They have been ignored, or old ones used instead.

我是菜我的记忆只有七秒
遇到的问题,找了半天,记录一下,不然下次又要重新搜。

参考资料:
Docker ubuntn 使用apt-get update报错
ubuntu 20.04 设置DNS
解决docker 容器内部无法使用ping命令,报错ping command not found的问题

问题描述

运行多个容器时,本来之前浏览器发http请求是可以的,突然显示连不上了
docker容器W: Some index files failed to download. They have been ignored, or old ones used instead._第1张图片
意识到可能是容器之间的通信出了问题,就想进一个容器ping一下测试一下,结果先是报错 ping: not found,于是先执行 apt-get update 结果又报错:

root@c219c6dd66a6:/app# apt-get update
Ign:1 http://mirrors.ustc.edu.cn/ubuntu xenial InRelease
Ign:2 http://mirrors.ustc.edu.cn/ubuntu xenial-security InRelease
Ign:3 http://mirrors.ustc.edu.cn/ubuntu xenial-updates InRelease
Ign:4 http://mirrors.ustc.edu.cn/ubuntu xenial-proposed InRelease
Ign:5 http://mirrors.ustc.edu.cn/ubuntu xenial-backports InRelease
Ign:1 http://mirrors.ustc.edu.cn/ubuntu xenial InRelease
Ign:2 http://mirrors.ustc.edu.cn/ubuntu xenial-security InRelease
Ign:3 http://mirrors.ustc.edu.cn/ubuntu xenial-updates InRelease
Ign:4 http://mirrors.ustc.edu.cn/ubuntu xenial-proposed InRelease
Ign:5 http://mirrors.ustc.edu.cn/ubuntu xenial-backports InRelease
Ign:1 http://mirrors.ustc.edu.cn/ubuntu xenial InRelease
Ign:2 http://mirrors.ustc.edu.cn/ubuntu xenial-security InRelease
Ign:3 http://mirrors.ustc.edu.cn/ubuntu xenial-updates InRelease
Ign:4 http://mirrors.ustc.edu.cn/ubuntu xenial-proposed InRelease
Ign:5 http://mirrors.ustc.edu.cn/ubuntu xenial-backports InRelease
Err:1 http://mirrors.ustc.edu.cn/ubuntu xenial InRelease
  Temporary failure resolving 'mirrors.ustc.edu.cn'
Err:2 http://mirrors.ustc.edu.cn/ubuntu xenial-security InRelease
  Temporary failure resolving 'mirrors.ustc.edu.cn'
Err:3 http://mirrors.ustc.edu.cn/ubuntu xenial-updates InRelease
  Temporary failure resolving 'mirrors.ustc.edu.cn'
Err:4 http://mirrors.ustc.edu.cn/ubuntu xenial-proposed InRelease
  Temporary failure resolving 'mirrors.ustc.edu.cn'
Err:5 http://mirrors.ustc.edu.cn/ubuntu xenial-backports InRelease
  Temporary failure resolving 'mirrors.ustc.edu.cn'
Reading package lists... Done         
W: Failed to fetch http://mirrors.ustc.edu.cn/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Failed to fetch http://mirrors.ustc.edu.cn/ubuntu/dists/xenial-security/InRelease  Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Failed to fetch http://mirrors.ustc.edu.cn/ubuntu/dists/xenial-updates/InRelease  Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Failed to fetch http://mirrors.ustc.edu.cn/ubuntu/dists/xenial-proposed/InRelease  Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Failed to fetch http://mirrors.ustc.edu.cn/ubuntu/dists/xenial-backports/InRelease  Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Some index files failed to download. They have been ignored, or old ones used instead.

解决步骤记录

1. 查看容器和宿主机的DNS设置

分别查看容器和宿主机的DNS设置,容器和宿主机的DNS设置应该一致。

cat /etc/resolv.conf

我在我的Ubuntu虚拟机(宿主机)中输入上述命令得到的是:

nameserver 127.0.0.53
options edns0 trust-ad
search localdomain

然后运行一个容器,进入容器

docker container run --rm -d --name dn1 dn
docker container exec -it dn1 /bin/bash
或
docker container exec -it dn1 sh

在这个容器中查看的DNS设置是:

nameserver 192.168.59.2
search localdomain

两个DNS设置不一致。

2. 修改宿主机的DNS设置

在Ubuntu虚拟机终端中输入

sudo vi /etc/systemd/resolved.conf

其中有个#DNS取消注释输入下述或直接加一行:

DNS=114.114.114.114 8.8.8.8 8.8.4.4
sudo systemctl restart systemd-resolved.service
sudo mv /etc/resolv.conf /etc/resolv.conf.bak
sudo ln -s /run/systemd/resolve/resolv.conf /etc/

设置完成后再用命令 cat /etc/resolv.conf 查看DNS设置看到:

nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 8.8.4.4
# Too many DNS servers configured, the following entries may be ignored.
nameserver 192.168.59.2
search localdomain

在这里插入图片描述

3. 配置docker容器的DNS

docker DNS的设置可以通过启动容器时添加参数 --dns 8.8.8.8 来设置(本次有效)
或者通过 /etc/default/docker的DNS参数来进行永久设置,然后重启DOCKER生效。

在Ubuntu虚拟机终端中输入

export http_proxy="http://10.1.9.100:808/"
DOCKER_OPTS="--dns 210.83.210.155 --dns 127.0.1.1"
sudo service docker restart

设置完成后再重新进入容器,用命令 cat /etc/resolv.conf 查看DNS设置看到:

nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 8.8.4.4
# Too many DNS servers configured, the following entries may be ignored.
nameserver 192.168.59.2
search localdomain

容器和宿主机的DNS设置一致了。

4. 哼,再来ping

容器和宿主机的DNS设置一致之后,再在容器中输入:(5555终于不报错了

apt-get update
apt install iputils-ping
apt install net-tools

终于可以ping了,ping其他容器,ping baidu.com都是可以的,其他缺啥也能装啥了

我最开始连不上的http也好了。。我感觉我之前好像从来没设置过DNS啊,怎么之前没碰到这情况(也可能是我忘了吧毕竟我是菜

你可能感兴趣的:(docker,ubuntu,容器)