【Docker】在ubuntu容器内使用apt-get遇到网络连接错误的解决方案

问题

初学docker,拉取(pull)下来一个最基础的ubuntu镜像后,无法使用`apt-get‘下载新的命令,如vim等。

原因分析

可能是sources.list配置的安装源在国外,时常访问不到,因此需要将安装源更新为国内源。也有可能是其他原因。

方法一

使用exit退出容器,然后使用sudo docker stop 容器ID停止该容器,重新使用下面的命令进入,重点是--net=host

sudo docker run -it --name test_ubuntu --net=host ubuntu

然后更新一下apt-get

apt-get update

尝试下载其他命令

方法二

  1. 备份源安装源文件
cp /etc/apt/sources.list /etc/apt/sources.list.bak
  1. 查看ubuntu的发行版本
cat etc/issue
  1. echo命令将国内安装源写入安装源文件中。
    这里要注意,由于ubuntu版本不同,设置安装源要与安装版本一致。
22.04 lts -- jammy
18.04 lts -- bionic
16.04 lts -- xenial
14.04 lts -- trusty
echo 'deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse' >> /etc/apt/sources.list
echo 'deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse' >> /etc/apt/sources.list
echo 'deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse' >> /etc/apt/sources.list
echo 'deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse' >> /etc/apt/sources.list
  1. 更新一下apt-get
apt-get update
  1. 尝试下载其他命令
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个甜甜的大橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值