1. 程式人生 > 其它 >Docker:在Dockerfile寫入多行文字覆蓋內容

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