Docker:Dockerfile中的变量

通过关键字设置变量

通过关键字ARGENV设置变量

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中使用
posted @ 2021-08-25 13:45  Feng1024  阅读(2365)  评论(0编辑  收藏  举报