如何在Dockerfile中使用命令替换 [英] How do I use command substition in Dockerfile

查看:504
本文介绍了如何在Dockerfile中使用命令替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Dockerfile中,我需要使用命令替换来添加一些环境变量。我想设置

In my Dockerfile I need to use command substition to add some environment variables. I want to set

ENV PYTHONPATH /usr/local/$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')

但它不起作用。结果是

foo@bar:~$ echo $PYTHONPATH
/usr/local/$(python3 -c from distutils import sysconfig; print(sysconfig.get_python_lib()))

怎么了?

推荐答案

出了什么问题



$(.. 。)您尝试的命令替换是Bash,而Dockerfile不是Bash。因此docker不知道该怎么做,只是给docker的纯文本,docker只是喷出您原样写的内容。

What went wrong

The $( ... ) command substitution you attempted is for Bash, whereas the Dockerfile is not Bash. So docker doesn't know what to do with that, it's just plain text to docker, docker just spews out what you wrote as-is.

避免在构建过程中将值硬编码到Dockerfile中,而是动态地将设置或自定义变量更改为 PYTHONPATH ,也许 ARG ... -build-arg 码头工人功能可能最有用,与 ENV ... 以确保其持续存在。

To avoid hard-coding values into a Dockerfile, and instead, to dynamically change a setting or custom variable as PYTHONPATH during the build, perhaps the ARG ... , --build-arg docker features might be most helpful, in combination with ENV ... to ensure it persists.

在Dockerfile中:

Within your Dockerfile:

ARG PYTHON_PATH_ARG

ENV PYTHONPATH ${PYTHON_PATH_ARG}

在构建容器的Bash中:

In Bash where you build your container:

python_path="/usr/local$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')"

docker build --build-arg PYTHON_PATH_ARG=$python_path .



说明



根据文档,< a href = https://docs.docker.com/engine/reference/builder/#arg rel = nofollow noreferrer> ARG


ARG 指令定义了一个变量,用户可以在构建时将其传递给使用-build-arg< varname> =< value> 标志使用docker build命令构建。

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag.

因此,在Bash中我们首先:

So, in Bash we first:

python_path="/usr/local$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')"




  • $(...) Bash命令替换用于动态地组合Python路径值

  • this为了清楚起见,值暂时存储在Bash变量 $ python_path

    • $(...) Bash command substitution is used to dynamically put together a Python path value
    • this value is stored temporarily in a Bash variable $python_path for clarity
    • docker build --build-arg PYTHON_PATH_ARG=$python_path .
      




      • Bash变量 $ python_path 的值传递给docker的-build-arg PYTHON_PATH_ARG

        • Bash variable $python_path value is passed to docker's --build-arg PYTHON_PATH_ARG
        • 在Dockerfile中:

          Within the Dockerfile:

          ARG PYTHON_PATH_ARG
          




          • 因此 PYTHON_PATH_ARG 存储中的值--build-arg PYTHON_PATH_ARG ...

            • so PYTHON_PATH_ARG stores the value from --build-arg PYTHON_PATH_ARG...
            • ARG 变量不等于 ENV 变量,所以我们不能只做 ARG PYTHONPATH 并完成它。根据有关使用arg变量

              ARG variables are not equivalent to ENV variables, so we couldn't merely do ARG PYTHONPATH and be done with it. According to documentation about Using arg variables:


              ARG 变量不会作为 ENV 变量是。

              ARG variables are not persisted into the built image as ENV variables are.

              所以最后:

              ENV PYTHONPATH ${PYTHON_PATH_ARG}
              




              • 我们使用Dockerfile的 $ {...} 约定来获取 PYTHON_PATH_ARG 的值,并将其保存到原始位置名为 PYTHONPATH 环境变量

                • We use Dockerfile's ${...} convention to get the value of PYTHON_PATH_ARG, and save it to your originally named PYTHONPATH environment variable
                • 您最初写道:

                  ENV PYTHONPATH /usr/local/$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')
                  

                  我重新编写了Python路径查找部分作为Bash命令,并在我的计算机上进行了测试:

                  I re-wrote the Python path finding portion as a Bash command, and tested on my machine:

                  $ python_path="/usr/local/$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')"
                  
                  $ echo $python_path
                  /usr/local//usr/lib/python3/dist-packages
                  

                  注意有一个双斜杠 ... local // usr ... ,不确定是否会为您带来任何麻烦,取决于您在代码中的使用方式。

                  Notice there is a double forward slash ... local//usr ... , not sure if that will break anything for you, depends on how you use it in your code.

                  相反,我将其更改为:

                  $ python_path="/usr/local$(python3 -c 'from distutils import sysconfig; print(sysconfig.get_python_lib())')"
                  

                  结果:

                  $ echo $python_path
                  /usr/local/usr/lib/python3/dist-packages
                  

                  因此,此新代码将没有双正斜杠。

                  So this new code will have no double forward slashes.

                  这篇关于如何在Dockerfile中使用命令替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆