Docker从命令或文件中设置环境变量 [英] Docker set environment variable from command or file
问题描述
我需要设置一个环境变量CLASSPATH
.在该变量中,我需要设置命令的结果:
I need to set an environment variable CLASSPATH
. In that variable I need to set the result of a command:
hadoop classpath --glob
这将返回大量的Java库,并且都需要将它们设置为该CLASSPATH
变量.最大的问题是,我只能在docker构建完成后才能运行此命令,这意味着我必须在ENTRYPOINT
中执行此命令.但是我只是无法正常工作.我尝试了不同的方法:
This will return a ton of java libraries and they all need to get set into that CLASSPATH
variable. The big problem is that I can only run this command after the docker build is complete, meaning I have to do it in the ENTRYPOINT
. But I just cant get it working. I tried differen approaches:
ENTRYPOINT ["sh", "-c", "export CLASSPATH=$(hadoop classpath --glob) ...."
ENTRYPOINT ["sh", "-c", "set CLASSPATH=$(hadoop classpath --glob) ...."
ENTRYPOINT ["sh", "-c", "CLASSPATH=$(hadoop classpath --glob) ...."
ENTRYPOINT ["sh", "-c", "/bin/bash && export CLASSPATH=$(hadoop classpath --glob) ...."
但是没有任何作用.该命令本身正在运行,我使用以下命令对其进行了测试:
But nothing of it is working. The command itself is working, I tested it using:
ENTRYPOINT ["sh", "-c", "echo $(hadoop classpath --glob) >> /tmp/classpath.tmp ...."
此文件在启动后包含正确的内容.因此,设置和保留环境变量只是一个问题.我应该如何设置环境变量?通常您使用类似
This file contains the correct content after startup. So there is just a problem with setting and persisting the environment variable. How am I supposed to set the environment variable? Usually you use something like
ENV CLASSPATH="some classpath"
但是在这里我不能使用ENV
语句,因为它不会处理命令$(hadoop classpath --glob)
But here I cant use the ENV
statement since it wont process the command $(hadoop classpath --glob)
推荐答案
该问题的唯一可行解决方案是根本不使用Dockerfile
中的ENTRYPOINT
.
The only working solution for that problem was to not use an ENTRYPOINT
in the Dockerfile
at all.
- 创建entrypoint.sh
- 将所需的所有内容(例如导出)添加到该sh文件中
- 将dockerfile中的entrypoint.sh复制到
/
- 在docker run或dockercompose中执行entrypoint.sh
- Create entrypoint.sh
- Add all the stuff you need (exports for example) into that sh file
- Copy entrypoint.sh in dockerfile to
/
- Execut entrypoint.sh in docker run or dockercompose
注意:您需要通过. /entrypoint.sh
执行它.否则它将无法正常工作.
Attention: you need to execute it by . /entrypoint.sh
. Otherwise it wont work.
这篇关于Docker从命令或文件中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!