Docker ubuntn 使用apt-get update报错

在docker 容器中执行apt-get update有时候会报错,当然造成错误的原因有很多情况,具体情况具体分析,

APT Hash sum mismatch错误的常见解决方法总结这篇博客写的不错,在此感谢博主提供的思路。

以下是本人解决docker容器中执行apt-get update报错的解决办法(踩了好多坑,血淋淋的阿,终于好用了。):

root@33c5b2ae7edc:/# apt-get update
Err:1 http://archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.

<1> 首先配置docker容器的DNS

cat /etc/resolv.conf       该命令查看容器和宿主机的DNS设置,容器和宿主机的DNS设置应该一致。

docker DNS的设置可以通过启动容器时添加参数  --dns  8.8.8.8  来设置(本次有效)

或者通过 /etc/default/docker的DNS参数来进行永久设置,然后重启DOCKER生效,

     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

<2> 查看容器 /etc/apt/目录中是否有apt.conf文件,该文件配置了代理:

Acquire::http::proxy "http://10.1.9.100:8080/";
Acquire::ftp::proxy "ftp://10.1.9.100:8080/";
Acquire::https::proxy "https://10.1.9.100:8080/";

如果没有该文件,可以将宿主机中的apt.conf文件拷贝到容器对应目录中。

<3>对代理服务器进行设置:

还有些开发者遇到的问题,是因为公司的Proxy的设置。
如果读者朋友使用了Proxy,那么可以尝试,在容器的 /etc/apt/apt.conf.d/ 目录下创建一个名称为99fixbadproxy 的文件

$ sudo gedit /etc/apt/apt.conf.d/99fixbadproxy

把以下文本复制到上述文件中,然后保存退出:

Acquire::http::Pipeline-Depth 0;
Acquire::http::No-Cache true;
Acquire::BrokenProxy    true;

<4> 清cache缓存:

$ sudo apt-get clean  
$ sudo apt-get update --fix-missing 

<5> 可以更改APT源,网上有挺多的国内源,可以搜索更改办法。

你可能感兴趣的:(问题点杂记)