Docker:在Dockerfile寫入多行文本覆蓋內容


場景

由於原鏡像中的/etc/apt/sources.list文件使用的源較慢,需要修改為國內的阿里雲。可通過以下命令寫入:

RUN echo 'deb http://mirrors.aliyun.com/debian/ buster main non-free contrib\n\
deb http://mirrors.aliyun.com/debian-security buster/updates main\n\
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib\n\
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib\n'\
> /etc/apt/sources.list

注意:

  • 這里使用單引號',所以無法使用諸如$(lsb_release -cs)的邏輯,如需使用,修改為雙引號"
  • 此處無法使用$(lsb_release -cs)獲取版本號,因為一般鏡像沒有安裝software-properties-common
  • 如果想要在代碼中實時獲取真實的系統版本號(buster),需要使用以下命令:
    cat /etc/os-release | grep "VERSION_CODENAME" | awk -F '=' '{print $2}'
    

修改后的實時獲取真實系統版本號的代碼如下:

RUN echo "deb http://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep "VERSION_CODENAME" | awk -F '=' '{print $2}') main non-free contrib\n\

deb http://mirrors.aliyun.com/debian-security $(cat /etc/os-release | grep "VERSION_CODENAME" | awk -F '=' '{print $2}')/updates main\n\

deb http://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep "VERSION_CODENAME" | awk -F '=' '{print $2}')-updates main non-free contrib\n\

deb http://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep "VERSION_CODENAME" | awk -F '=' '{print $2}')-backports main non-free contrib\n"\
> /etc/apt/sources.list


免責聲明!

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



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