如果我设置了一个环境变量,比如ENV ADDRESSEE=world,我想在入口点脚本中使用它,连接成一个固定的字符串,比如:
ENTRYPOINT ["./greeting", "--message", "Hello, world!"]
world是环境变量的值,我该怎么做呢?我尝试使用“你好,$ADDRESSEE”,但这似乎不工作,因为它需要$ADDRESSEE字面上。
如果我设置了一个环境变量,比如ENV ADDRESSEE=world,我想在入口点脚本中使用它,连接成一个固定的字符串,比如:
ENTRYPOINT ["./greeting", "--message", "Hello, world!"]
world是环境变量的值,我该怎么做呢?我尝试使用“你好,$ADDRESSEE”,但这似乎不工作,因为它需要$ADDRESSEE字面上。
当前回答
You're using the exec form of ENTRYPOINT. Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.(from Dockerfile reference)
在你的例子中,我会用壳层形式
ENTRYPOINT ./greeting --message "Hello, $ADDRESSEE\!"
其他回答
如果有人想要传递一个ARG或ENV变量到ENTRYPOINT的exec形式,那么可能会使用在映像构建过程中创建的临时文件。
在我的情况下,我必须启动不同的应用程序,这取决于。net应用程序是否已发布为自包含的。 我所做的是创建临时文件,并在bash脚本的if语句中使用它的名称。
dockerfile的一部分:
ARG SELF_CONTAINED=true #ENV SELF_CONTAINED=true also works
# File has to be used as a variable as it's impossible to pass variable do ENTRYPOINT using Exec form. File name allows to check whether app is self-contained
RUN touch ${SELF_CONTAINED}.txt
COPY run-dotnet-app.sh .
ENTRYPOINT ["./run-dotnet-app.sh", "MyApp" ]
run-dotnet-app.sh:
#!/bin/sh
FILENAME=$1
if [ -f "true.txt" ]; then
./"${FILENAME}"
else
dotnet "${FILENAME}".dll
fi
You're using the exec form of ENTRYPOINT. Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.(from Dockerfile reference)
在你的例子中,我会用壳层形式
ENTRYPOINT ./greeting --message "Hello, $ADDRESSEE\!"
我试图用建议的答案来解决问题,但仍然遇到了一些问题……
这是我问题的解决方案:
ARG APP_EXE="AppName.exe"
ENV _EXE=${APP_EXE}
# Build a shell script because the ENTRYPOINT command doesn't like using ENV
RUN echo "#!/bin/bash \n mono ${_EXE}" > ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
# Run the generated shell script.
ENTRYPOINT ["./entrypoint.sh"]
特别针对你的问题:
RUN echo "#!/bin/bash \n ./greeting --message ${ADDRESSEE}" > ./entrypoint.sh
RUN chmod +x ./entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
以下是对我有效的方法:
ENTRYPOINT [ "/bin/bash", "-c", "source ~/.bashrc && ./entrypoint.sh ${@}", "--" ]
现在您可以向docker run命令提供任何参数,同时仍然可以读取所有环境变量。
对于我来说,我希望将脚本的名称存储在一个变量中,并且仍然使用exec表单。
注意:确保您试图使用的变量通过命令行或ENV指令声明为环境变量。
最初我是这样做的:
ENTRYPOINT [ "${BASE_FOLDER}/scripts/entrypoint.sh" ]
但显然这行不通,因为我们使用的是shell形式,列出的第一个程序需要是PATH上的可执行文件。所以为了解决这个问题,这就是我最后做的:
ENTRYPOINT [ "/bin/bash", "-c", "exec ${BASE_FOLDER}/scripts/entrypoint.sh \"${@}\"", "--" ]
注意,双引号是必需的
这样做的目的是允许我们接受传递给/bin/bash的任何额外参数,并在名称被bash解析后将这些相同的参数提供给我们的脚本。
男人7狂欢
——A——表示选项结束并进一步禁用 选择处理。之后的任何争论都会被处理 作为文件名和参数。是的论点 相当于——。