学习Docker在Ubuntu容器中使用apt-get update命令遇到如下网络连接错误:

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

搜索各种解决方案,改容器dns的,改容器ip的都有,过程都比较复杂,要想办法在容器内修改配置文件,或在容器外改好在通过数据绑定传入容器。

最后发现有人通过--net=host参数改变容器网络模式解决,实验后果然解决问题。

$ docker run -it --name test --net=host ubuntu
root@localhost:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [839 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]        
Get:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]       
Get:6 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]  
Get:7 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [44.6 kB]
Get:8 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [8213 B]
Get:9 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [889 kB]     
Get:10 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]      
Get:11 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]                                                           
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [12.6 kB]                                             
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [59.0 kB]                                             
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [1183 kB]                                                   
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1371 kB]                                               
Get:17 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [4247 B]                                              
Get:18 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [2496 B]                                                  
Fetched 17.8 MB in 8s (2116 kB/s)                                                                                                      
Reading package lists... Done
root@localhost:/#