博客
关于我
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/

你可能感兴趣的文章
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>