場景
由於原鏡像中的/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