Docker容器内更新apt-get国内加速实现过程
更新时间:2026年03月01日 10:25:46 作者:Amewin
文章讲述了使用国内镜像加速Docker容器内apt-get的方法,通过修改配置文件指向国外镜像源,解决了网速慢的问题
Docker容器内更新源apt-get
由于不使用国内镜像网速缓慢,所以使用国内镜像加速就很必要了,但是经过博主测试大部分apt-get加速都是针对Ubuntu 的,根本解决不了Docker 容器内 apt-get 加速问题。
解决过程
进过博主反复尝试终于找到了解决方案。
#1.进入容器 docker exec -it <容器名称/id> bash #2.执行命令apt-get update 发现缓慢 #3.进入apt-get 配置目录 cd / /apt #4.执行备份命令 --避免修改失败无法使用 cp sources.list sources.list.bak #5.同时执行echo下的4行命令,修改成国内镜像源 echo "">sources.list echo "deb http://ftp2.cn.debian.org/debian/ buster main">>sources.list echo "deb http://ftp2.cn.debian.org/debian/debian-security buster/updates main">>sources.list echo "deb http://ftp2.cn.debian.org/debian/debian buster-updates main">>sources.list #6.查看文件 是否修改成功 cat sources.list ############################### root@62be94cc90e7:/etc/apt# cat sources.list deb http://ftp2.cn.debian.org/debian/ buster main deb http://ftp2.cn.debian.org/debian/debian-security buster/updates main deb http://ftp2.cn.debian.org/debian/debian buster-updates main root@62be94cc90e7:/etc/apt# ############################### #7.更新apt-get apt-get update #修改完成 #如果发现安装vim 缓慢或者失败,说明本教程不适合你 apt-get install vim #将备份完成的文件改回来,即可。 rm sources.list mv sources.list.bak sources.list
说明
修改的原理是使用地址
https://www.debian.org/mirror/list

在别的国家地区也是可以使用该方法加速,将配置改成所在地区网址即可。
root@62be94cc90e7:/etc/apt# apt-get update Get:1 http://mirrors.tuna.tsinghua.edu.cn/debian buster InRelease [122 kB] Ign:2 http://mirrors.tuna.tsinghua.edu.cn/debian/debian-security buster/updates InRelease Ign:3 http://mirrors.tuna.tsinghua.edu.cn/debian/debian buster-updates InRelease Err:4 http://mirrors.tuna.tsinghua.edu.cn/debian/debian-security buster/updates Release 404 Not Found [IP: 101.6.8.193 80] Get:6 http://mirrors.tuna.tsinghua.edu.cn/debian buster/main amd64 Packages [7906 kB] Err:5 http://mirrors.tuna.tsinghua.edu.cn/debian/debian buster-updates Release 404 Not Found [IP: 101.6.8.193 80] Reading package lists... Done E: The repository 'http://ftp2.cn.debian.org/debian/debian-security buster/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://ftp2.cn.debian.org/debian/debian buster-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.
详细文档(避免新手出错)
#进入容器 docker exec -it <容器名称/id> bash #执行命令apt-get 发现缓慢 apt-get update #执行命令 Ctrl + C 停止 返回命令行 cd /ect/apt #查看目录 ls root@62be94cc90e7:/etc/apt# ls apt.conf.d auth.conf.d preferences.d sources.list sources.list.d trusted.gpg.d #执行备份命令 --避免修改失败无法使用,备份可恢复 cp sources.list sources.list.bak #执行命令 更改apt-get 不用安装vim (vim真的太慢了) echo "">sources.list echo "deb http://ftp2.cn.debian.org/debian/ buster main">>sources.list echo "deb http://ftp2.cn.debian.org/debian/debian-security buster/updates main">>sources.list echo "deb http://ftp2.cn.debian.org/debian/debian buster-updates main">>sources.list #更改完成后执行命令 apt-get update #将备份完成的文件改回来,即可。 #rm --删除文件后使用 mv --更改文件名字 rm sources.list mv sources.list.bak sources.list
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
使用Docker搭建Django,Nginx,R,Python部署环境的方法
本篇文章主要介绍了使用Docker搭建Django,Nginx,R,Python部署环境的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-02-02
使用docker compose安装FastDfs文件服务器的实例详解
这篇文章主要介绍了使用docker compose安装FastDfs文件服务器的实例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-10-10
docker启动mysql及-e MYSQL_ROOT_PASSWORD=my-secret-pw问题解决
本文主要介绍了docker启动mysql及-e MYSQL_ROOT_PASSWORD=my-secret-pw问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-07-07


最新评论