在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源,網上有挺多的國內源,可以搜索更改辦法。