Dockerfile的ENV替换和转义符

一 Dockerfile中的ENV指令用以定义镜像的环境变量
示例如下:
RUN set -ex && apt-get update && apt-get install -y iputils-ping  
ENV PATH /usr/local/bin:$PATH  
ENV LANG C.UTF-8  
ENV TERM xterm  
ENV PYTHON_VERSION 3.5.3  
ENV name1=ping name2=on_ip  
CMD $name1 $name2  
说明:定义环境变量的同时,可以引用已经定义的环境变量。
在ENV指令中,可以直接引用如下环境变量:
HOME:用户主目录
HOSTNAME:默认容器的主机名
PATH:环境路径
TERM:默认xterm

二 由于镜像的层次文件系统,ENV定义的环境变量在后续层次中才能够被应用
示例如下:
ENV abc=hello  
ENV abc=bye def=$abc  
ENV ghi=$abc  
说明:
上述定义的结果中,def=hello,ghi=bye

三 启动容器后,在容器实例中,可以通过env命令查看环境变量
env

四 实战1
1 Dockerfile文件内容
#escape=\
#thie is a Dockerfile
FROM busybox
ENV box /hello
ENV a1 ${box}_a1
ENV a2 $box_a1
ENV a3 $box
ENV a4 ${box}
ENV a5 ${a1:-world}
ENV a6 ${a0:-world}
ENV a7 ${a1:+world}
ENV a8 ${a0:+world}
ENV a9 \$box
ENV a10 \${box}
2 测试
[root@localhost df]# docker build -t vker/df:0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/12 : FROM busybox
---> 6ad733544a63
Step 2/12 : ENV box /hello
---> Running in d02799e03853
---> 42ce9cb6ff2d
Removing intermediate container d02799e03853
Step 3/12 : ENV a1 ${box}_a1
---> Running in 2da6572101ea
---> b0c48ad6493f
Removing intermediate container 2da6572101ea
Step 4/12 : ENV a2 $box_a1
---> Running in ecfcef134413
---> 01b1c31bafeb
Removing intermediate container ecfcef134413
Step 5/12 : ENV a3 $box
---> Running in 0b99c236d291
---> c23b77d0dc2b
Removing intermediate container 0b99c236d291
Step 6/12 : ENV a4 ${box}
---> Running in 857f46adaa4b
---> 96bc86cb4b4e
Removing intermediate container 857f46adaa4b
Step 7/12 : ENV a5 ${a1:-world}
---> Running in c2c28de1df9c
---> 6b06e0e7e3d2
Removing intermediate container c2c28de1df9c
Step 8/12 : ENV a6 ${a0:-world}
---> Running in d98a96c77096
---> 9ee5a36f5cff
Removing intermediate container d98a96c77096
Step 9/12 : ENV a7 ${a1:+world}
---> Running in cccb508fea54
---> 74e8b22013bc
Removing intermediate container cccb508fea54
Step 10/12 : ENV a8 ${a0:+world}
---> Running in ef18b0a3b373
---> 6a58987ed19b
Removing intermediate container ef18b0a3b373
Step 11/12 : ENV a9 \$box
---> Running in 8b58200ba08e
---> 98eb20ae4275
Removing intermediate container 8b58200ba08e
Step 12/12 : ENV a10 \${box}
---> Running in e885b683320e
---> 2a25e714317f
Removing intermediate container e885b683320e
Successfully built 2a25e714317f
Successfully tagged vker/df:0.1
[root@localhost df]# docker run -it vker/df:0.1
/ # env
HOSTNAME=da1714f4cec7
SHLVL=1
HOME=/root
TERM=xterm
a1=/hello_a1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
a2=
a3=/hello
a4=/hello
a5=/hello_a1
box=/hello
a6=world
a7=world
a8=
a9=$box
a10=${box}
PWD=/
五 实战2
1 Dockerfile
#escape=`
#thie is a Dockerfile
FROM busybox
ENV box /hello
ENV a1 ${box}_a1
ENV a2 $box_a1
ENV a3 $box
ENV a4 ${box}
ENV a5 ${a1:-world}
ENV a6 ${a0:-world}
ENV a7 ${a1:+world}
ENV a8 ${a0:+world}
ENV a9 \$box
ENV a10 \${box}
2 测试
[root@localhost df]# docker build -t vker/df:0.2 .
Sending build context to Docker daemon  2.048kB
Step 1/12 : FROM busybox
---> 6ad733544a63
Step 2/12 : ENV box /hello
---> Using cache
---> 42ce9cb6ff2d
Step 3/12 : ENV a1 ${box}_a1
---> Using cache
---> b0c48ad6493f
Step 4/12 : ENV a2 $box_a1
---> Using cache
---> 01b1c31bafeb
Step 5/12 : ENV a3 $box
---> Using cache
---> c23b77d0dc2b
Step 6/12 : ENV a4 ${box}
---> Using cache
---> 96bc86cb4b4e
Step 7/12 : ENV a5 ${a1:-world}
---> Using cache
---> 6b06e0e7e3d2
Step 8/12 : ENV a6 ${a0:-world}
---> Using cache
---> 9ee5a36f5cff
Step 9/12 : ENV a7 ${a1:+world}
---> Using cache
---> 74e8b22013bc
Step 10/12 : ENV a8 ${a0:+world}
---> Using cache
---> 6a58987ed19b
Step 11/12 : ENV a9 \$box
---> Running in c9e0ec549370
---> 580f54c68259
Removing intermediate container c9e0ec549370
Step 12/12 : ENV a10 \${box}
---> Running in 10db56b4ab60
---> 9d7e21bc1282
Removing intermediate container 10db56b4ab60
Successfully built 9d7e21bc1282
Successfully tagged vker/df:0.2
[root@localhost df]# docker run -it vker/df:0.2
/ # env
HOSTNAME=2fc25e990f12
SHLVL=1
HOME=/root
TERM=xterm
a1=/hello_a1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
a2=
a3=/hello
a4=/hello
a5=/hello_a1
box=/hello
a6=world
a7=world
a8=
a9=\/hello
a10=\/hello


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值