通過關鍵字設置變量
通過關鍵字ARG
,ENV
設置變量
ARG arg1=test
ENV env1=production
注意:
- 不能通過表達如
$(uname -a)
進行設置,只能設置為常量 - 其中的差別,可以這么理解:
- ARG設置的變量在構建完成后,就會丟失。即在Docker中無法引用該變量
- ENV設置的變量在Docker中可以通過如
${env1}
訪問
在RUN中設置變量
在RUN通過arg=someValue
中設置變量,以下腳本先獲取Debain的系統版本號,並設置到了os_release
變量中,在后續的命令中可以通過${os_release}
進行引用
RUN os_release="$(cat /etc/os-release | grep VERSION_CODENAME | awk -F '=' '{print $2}')" &&\
echo "deb http://mirrors.aliyun.com/debian/ ${os_release} main non-free contrib\n\
deb http://mirrors.aliyun.com/debian-security ${os_release}/updates main\n\
deb http://mirrors.aliyun.com/debian/ ${os_release}-updates main non-free contrib\n\
deb http://mirrors.aliyun.com/debian/ ${os_release}-backports main non-free contrib\n"\
> /etc/apt/sources.list
注意:
- 一個RUN命令,相當於新打開一個Shell。所以上一個RUN設置的變量無法在下一個RUN中使用。