解决docker容器不能连接网络

1、问题描述:

  • 新建docker容器
    docker run -it --rm ubuntu:18.04
    
  • 执行apt update,报错:
    bash: ping: command not found
    root@d087aa834821:/# apt update
    Ign:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
    Ign:2 http://archive.ubuntu.com/ubuntu bionic InRelease
    Ign:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
    Err:4 http://security.ubuntu.com/ubuntu bionic-security Release
      404   [IP: 91.189.91.38 80]
    Ign:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
    Err:6 http://archive.ubuntu.com/ubuntu bionic Release
      404   [IP: 91.189.88.142 80]
    Err:7 http://archive.ubuntu.com/ubuntu bionic-updates Release
      404   [IP: 91.189.88.142 80]
    Err:8 http://archive.ubuntu.com/ubuntu bionic-backports Release
      404   [IP: 91.189.88.142 80]
    Reading package lists... Done
    E: The repository 'http://security.ubuntu.com/ubuntu bionic-security Release' does not have a Release file.
    N: Updating from such a repository can't be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.
    E: The repository 'http://archive.ubuntu.com/ubuntu bionic Release' does not have a Release file.
    N: Updating from such a repository can't be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.
    E: The repository 'http://archive.ubuntu.com/ubuntu bionic-updates Release' does not have a Release file.
    N: Updating from such a repository can't be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.
    E: The repository 'http://archive.ubuntu.com/ubuntu bionic-backports Release' does not have a Release file.
    N: Updating from such a repository can't be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.
    

2、解决方法:


方法1:新建docker时指定参数--network=host

docker run -itd --rm --network=host ubuntu:18.04 /bin/bash

方法2:重置docker0( docker0为docker的默认网桥)

# 杀死docker进程
pkill docker 
# 清空nat表的所有链
iptables -t nat -F
#停止docker默认网桥
ifconfig docker0 down
#删除docker默认网桥
brctl delbr docker0 
# 
docker -d 
#重启docker
systemctl restart docker
  • 查看网络基本信息

    ifconfig
    
  • 查看网桥

    brctl show
    

方法3:修改docker默认配置文件

  • 查看文件/etc/default/docker

    vi /etc/default/docker
    
  • 文件内容中的DOCKER_OPTS选项为:

    DOCKER_OPTS =“-dns 8.8.8.8 –dns 8.8.4.4”
    
  • 查看本机dns

    cat /etc/resolv.conf
    
  • 输出为:

    # This file is managed by man:systemd-resolved(8). Do not edit.
    #
    # This is a dynamic resolv.conf file for connecting local clients to the
    # internal DNS stub resolver of systemd-resolved. This file lists all
    # configured search domains.
    #
    # Run "systemd-resolve --status" to see details about the uplink DNS servers
    # currently in use.
    #
    # Third party programs must not access this file directly, but only through the
    # symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
    # replace this symlink by a static file or a different symlink.
    #
    # See man:systemd-resolved.service(8) for details about the supported modes of
    # operation for /etc/resolv.conf.
    
    nameserver 127.0.0.53
    options edns0
    

    很明显docker参数中的dns地址与本机实际地址不一致,此时只需要注释掉DOCKER_OPTS参数即可。

  • 重启docker

    systemctl restart docker
    
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值