前言
我們從 DockerHub 上拉取鏡像、創建容器以實現各種各樣的功能。但是由於大部分的 Docker 鏡像都是默認國外的軟件源,下載更新非常慢,本文講述如何更改 Docker 容器軟件源。
說明
更改 Docker 容器軟件源的步驟大部分同獨立的 Linux 系統類似。
執行步驟:
- 備份配置文件;
- 選取對應國內源;
- 編輯配置文件;
- 更新軟件源。
唯一的區別是 “編輯配置文件” 一步:
-
Docker 容器中沒有默認加載文本編輯器,無法直接編輯配置文件。
包括 vi 、 vim 、 nano 等等都沒有安裝。
使用 shell 重定向
我們可以通過 shell 提供的重定向功能輸出文件。
- shell 重定向的具體使用,請閱讀:菜鳥教程 Shell 輸入/輸出重定向。
使用 echo 調用重定向
命令框架如下:
echo -e "xxxxxxx\nxxxxxxx" >> sources.list
-
“echo” :輸出字符串;
-
“-e” :開啟轉義。
若無該選項,“\n” 按字符輸出,不換行。
-
“ "xxxxxxx\nxxxxxxx" ” :用英文雙引號來包括要替換鏡像源信息,“\n” 換行符;
-
“>>” :重定向;
-
“sources.list” :重定向輸出的文件。
我們以 debian 為例,使用具體命令。
echo -e "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free\ndeb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free\ndeb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free\ndeb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free" >> sources.list
然后 apt update
更新軟件源即可。