Docker 容器更换软件源

前言

我们从 DockerHub 上拉取镜像、创建容器以实现各种各样的功能。但是由于大部分的 Docker 镜像都是默认国外的软件源,下载更新非常慢,本文讲述如何更改 Docker 容器软件源。

说明

更改 Docker 容器软件源的步骤大部分同独立的 Linux 系统类似。

执行步骤:

  • 备份配置文件;
  • 选取对应国内源;
  • 编辑配置文件;
  • 更新软件源。

唯一的区别是 “编辑配置文件” 一步:

  • Docker 容器中没有默认加载文本编辑器,无法直接编辑配置文件。

    包括 vi 、 vim 、 nano 等等都没有安装。

使用 shell 重定向

我们可以通过 shell 提供的重定向功能输出文件。

使用 echo 调用重定向

命令框架如下:

echo -e "xxxxxxx
xxxxxxx" >> sources.list
  • “echo” :输出字符串;

  • “-e” :开启转义。

    若无该选项,“ ” 按字符输出,不换行。

  • “ "xxxxxxx xxxxxxx" ” :用英文双引号来包括要替换镜像源信息,“ ” 换行符;

  • “>>” :重定向;

  • “sources.list” :重定向输出的文件。

我们以 debian 为例,使用具体命令。

echo -e "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free" >> sources.list

然后 apt update 更新软件源即可。

原文地址:https://www.cnblogs.com/Yogile/p/12625532.html