Docker 容器更換軟件源


前言

我們從 DockerHub 上拉取鏡像、創建容器以實現各種各樣的功能。但是由於大部分的 Docker 鏡像都是默認國外的軟件源,下載更新非常慢,本文講述如何更改 Docker 容器軟件源。

說明

更改 Docker 容器軟件源的步驟大部分同獨立的 Linux 系統類似。

執行步驟:

  • 備份配置文件;
  • 選取對應國內源;
  • 編輯配置文件;
  • 更新軟件源。

唯一的區別是 “編輯配置文件” 一步:

  • Docker 容器中沒有默認加載文本編輯器,無法直接編輯配置文件。

    包括 vi 、 vim 、 nano 等等都沒有安裝。

使用 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 更新軟件源即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM