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安装java环境的实现步骤

    docker安装java环境的实现步骤

    这篇文章主要介绍了docker安装java环境的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 使用Docker搭建Django,Nginx,R,Python部署环境的方法

    使用Docker搭建Django,Nginx,R,Python部署环境的方法

    本篇文章主要介绍了使用Docker搭建Django,Nginx,R,Python部署环境的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 使用Docker部署Python Flask应用的完整教程

    使用Docker部署Python Flask应用的完整教程

    Docker是一种开源的容器化平台,可以将应用程序及其依赖项打包成一个独立的容器,实现快速部署和跨平台运行,本文将详细介绍如何使用Docker来部署Python Flask应用程序,帮助开发者更高效地构建和部署应用,需要的朋友可以参考下
    2024-06-06
  • 使用docker compose安装FastDfs文件服务器的实例详解

    使用docker compose安装FastDfs文件服务器的实例详解

    这篇文章主要介绍了使用docker compose安装FastDfs文件服务器的实例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • docker 打包本地镜像,并到其他机器进行恢复操作

    docker 打包本地镜像,并到其他机器进行恢复操作

    这篇文章主要介绍了docker 打包本地镜像,并到其他机器进行恢复操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • 更换k8s容器运行时环境为docker的过程

    更换k8s容器运行时环境为docker的过程

    这篇文章主要介绍了更换k8s容器运行时环境为docker的过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-10-10
  • docker启动mysql及-e MYSQL_ROOT_PASSWORD=my-secret-pw问题解决

    docker启动mysql及-e MYSQL_ROOT_PASSWORD=my-secret-pw问题解决

    本文主要介绍了docker启动mysql及-e MYSQL_ROOT_PASSWORD=my-secret-pw问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 关于docker中 WSL 配置与修改问题

    关于docker中 WSL 配置与修改问题

    这篇文章主要介绍了docker中 WSL 配置与修改,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • CentOS7  docker服务无法启动解决方案及实现步骤

    CentOS7 docker服务无法启动解决方案及实现步骤

    这篇文章主要介绍了CentOS7 docker服务无法启动解决方案及实现步骤的相关资料,这里对Docker 服务无法启动做了详细的排查工作及流程,并附解决办法,需要的朋友可以参考下
    2016-12-12
  • Docker实践之搭建wordpress的方法

    Docker实践之搭建wordpress的方法

    这篇文章主要介绍了Docker实践之搭建wordpress的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12

最新评论