docker apt-get update失败问题解决

发布时间 2023-06-02 13:51:01作者: 我超凶

一、问题描述
docker容器相当于linux系统的精简版,内部很多指令是无法直接使用的,例如vim指令,为了使用vim指令,我们需要进入容器内部进行安装,安装步骤为:

apt-get update 
apt-get install vim 

很多时候我们发现安装会失败,这里是由于下载源问题。
二、解决方案
1.进入宿主机下
cd /etc/docker

编辑daemon.json文件,改为如下

{
  "dns": ["8.8.8.8","114.114.114.114"]
}

2.重启docker
systemctl restart docker
3.进入需要安装指令的容器内部
docker exec -it xxx /bin/bash
4.执行apt-get update 命令可能会出错

执行apt-key adv添加
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
5.docker容器内部进入/etc/apt/目录

// 进入apt目录
cd /etc/apt/
// 备份 source.list
cp source.list source.list2
// 执行下面命令添加apt镜像源
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list

6.再次执行以下命令,如果失败多执行几次

apt-get update 
apt-get install vim