博客
关于我
Dockerfile的ENV替换和转义符
阅读量:173 次
发布时间:2019-02-28

本文共 3965 字,大约阅读时间需要 13 分钟。

一 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 DockerfileFROM busyboxENV box /helloENV a1 ${box}_a1ENV a2 $box_a1ENV a3 $boxENV a4 ${box}ENV a5 ${a1:-world}ENV a6 ${a0:-world}ENV a7 ${a1:+world}ENV a8 ${a0:+world}ENV a9 \$boxENV a10 \${box}
2 测试
[root@localhost df]# docker build -t vker/df:0.1 .Sending build context to Docker daemon  2.048kBStep 1/12 : FROM busybox---> 6ad733544a63Step 2/12 : ENV box /hello---> Running in d02799e03853---> 42ce9cb6ff2dRemoving intermediate container d02799e03853Step 3/12 : ENV a1 ${box}_a1---> Running in 2da6572101ea---> b0c48ad6493fRemoving intermediate container 2da6572101eaStep 4/12 : ENV a2 $box_a1---> Running in ecfcef134413---> 01b1c31bafebRemoving intermediate container ecfcef134413Step 5/12 : ENV a3 $box---> Running in 0b99c236d291---> c23b77d0dc2bRemoving intermediate container 0b99c236d291Step 6/12 : ENV a4 ${box}---> Running in 857f46adaa4b---> 96bc86cb4b4eRemoving intermediate container 857f46adaa4bStep 7/12 : ENV a5 ${a1:-world}---> Running in c2c28de1df9c---> 6b06e0e7e3d2Removing intermediate container c2c28de1df9cStep 8/12 : ENV a6 ${a0:-world}---> Running in d98a96c77096---> 9ee5a36f5cffRemoving intermediate container d98a96c77096Step 9/12 : ENV a7 ${a1:+world}---> Running in cccb508fea54---> 74e8b22013bcRemoving intermediate container cccb508fea54Step 10/12 : ENV a8 ${a0:+world}---> Running in ef18b0a3b373---> 6a58987ed19bRemoving intermediate container ef18b0a3b373Step 11/12 : ENV a9 \$box---> Running in 8b58200ba08e---> 98eb20ae4275Removing intermediate container 8b58200ba08eStep 12/12 : ENV a10 \${box}---> Running in e885b683320e---> 2a25e714317fRemoving intermediate container e885b683320eSuccessfully built 2a25e714317fSuccessfully tagged vker/df:0.1[root@localhost df]# docker run -it vker/df:0.1/ # envHOSTNAME=da1714f4cec7SHLVL=1HOME=/rootTERM=xterma1=/hello_a1PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bina2=a3=/helloa4=/helloa5=/hello_a1box=/helloa6=worlda7=worlda8=a9=$boxa10=${box}PWD=/
五 实战2
1 Dockerfile
#escape=`#thie is a DockerfileFROM busyboxENV box /helloENV a1 ${box}_a1ENV a2 $box_a1ENV a3 $boxENV a4 ${box}ENV a5 ${a1:-world}ENV a6 ${a0:-world}ENV a7 ${a1:+world}ENV a8 ${a0:+world}ENV a9 \$boxENV a10 \${box}
2 测试
[root@localhost df]# docker build -t vker/df:0.2 .Sending build context to Docker daemon  2.048kBStep 1/12 : FROM busybox---> 6ad733544a63Step 2/12 : ENV box /hello---> Using cache---> 42ce9cb6ff2dStep 3/12 : ENV a1 ${box}_a1---> Using cache---> b0c48ad6493fStep 4/12 : ENV a2 $box_a1---> Using cache---> 01b1c31bafebStep 5/12 : ENV a3 $box---> Using cache---> c23b77d0dc2bStep 6/12 : ENV a4 ${box}---> Using cache---> 96bc86cb4b4eStep 7/12 : ENV a5 ${a1:-world}---> Using cache---> 6b06e0e7e3d2Step 8/12 : ENV a6 ${a0:-world}---> Using cache---> 9ee5a36f5cffStep 9/12 : ENV a7 ${a1:+world}---> Using cache---> 74e8b22013bcStep 10/12 : ENV a8 ${a0:+world}---> Using cache---> 6a58987ed19bStep 11/12 : ENV a9 \$box---> Running in c9e0ec549370---> 580f54c68259Removing intermediate container c9e0ec549370Step 12/12 : ENV a10 \${box}---> Running in 10db56b4ab60---> 9d7e21bc1282Removing intermediate container 10db56b4ab60Successfully built 9d7e21bc1282Successfully tagged vker/df:0.2[root@localhost df]# docker run -it vker/df:0.2/ # envHOSTNAME=2fc25e990f12SHLVL=1HOME=/rootTERM=xterma1=/hello_a1PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bina2=a3=/helloa4=/helloa5=/hello_a1box=/helloa6=worlda7=worlda8=a9=\/helloa10=\/hello

转载地址:http://wjej.baihongyu.com/

你可能感兴趣的文章
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>