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

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

Dockerfile中的ENV指令详解

Dockerfile是定义容器镜像的文件,而ENV命令用于在镜像中定义环境变量。环境变量在容器运行时非常有用,可以配置应用程序、设置路径或自定义信息。在Dockerfile中,ENV命令的作用是为镜像中的容器提供必要的环境配置。

ENV命令的基本用法

ENV key value

ENV命令用于定义镜像的环境变量,格式为键值对。键是环境变量的名称,值是变量的值。定义的环境变量可以在容器运行时通过env命令查看。

ENV命令的示例

ENV PATH /usr/local/bin:$PATHENV LANG C.UTF-8ENV TERM xtermENV PYTHON_VERSION 3.5.3ENV name1=ping name2=on_ipCMD $name1 $name2

在上述示例中:

  • ENV PATH /usr/local/bin:$PATH - 定义了PATH环境变量,添加了/usr/local/bin到路径中。
  • ENV LANG C.UTF-8 - 设置容器的默认语言为UTF-8。
  • ENV TERM xterm - 设置容器的终端类型为xterm。
  • ENV PYTHON_VERSION 3.5.3 - 设置Python的版本。
  • ENV name1=ping name2=on_ip - 定义了两个环境变量。
  • CMD $name1 $name2 - 启动容器时执行ping和on_ip命令。

ENV命令的优先级

由于镜像的层次文件系统,ENV命令的定义会按照从下往上依次生效。也就是说,镜像的最底层定义的环境变量会被上层镜像覆盖。

ENV命令的引用

ENV命令支持引用前面定义的环境变量。例如:

ENV abc=helloENV abc=bye def=$abcENV ghi=$abc

定义的结果中:

  • def=hello
  • ghi=bye

这里的$abc表示引用前面定义的abc环境变量的值。

实战1: Dockerfile文件内容

FROM 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}

测试步骤

  • 构建镜像:
  • docker build -t vker/df:0.1 .
    1. 启动容器并查看环境变量:
    2. docker run -it vker/df:0.1 /env

      运行结果显示:

      • HOSTNAME=da1714f4cec7
      • SHLVL=1
      • HOME=/root
      • TERM=xterm
      • a1=/hello_a1
      • a2=world
      • a3=/hello
      • a4=/hello
      • a5=/hello_a1
      • a6=world
      • a7=world
      • a8=world
      • a9=/hello
      • a10=/hello

      实战2: Dockerfile文件内容

      FROM 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}

      测试步骤

    3. 构建镜像:
    4. docker build -t vker/df:0.2 .
      1. 启动容器并查看环境变量:
      2. docker run -it vker/df:0.2 /env

        运行结果显示:

        • HOSTNAME=2fc25e990f12
        • SHLVL=1
        • HOME=/root
        • TERM=xterm
        • a1=/hello_a1
        • a2=world
        • a3=/hello
        • a4=/hello
        • a5=/hello_a1
        • a6=world
        • a7=world
        • a8=world
        • a9=/hello
        • a10=/hello

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

    你可能感兴趣的文章
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>