如果我设置了一个环境变量,比如ENV ADDRESSEE=world,我想在入口点脚本中使用它,连接成一个固定的字符串,比如:
ENTRYPOINT ["./greeting", "--message", "Hello, world!"]
world是环境变量的值,我该怎么做呢?我尝试使用“你好,$ADDRESSEE”,但这似乎不工作,因为它需要$ADDRESSEE字面上。
如果我设置了一个环境变量,比如ENV ADDRESSEE=world,我想在入口点脚本中使用它,连接成一个固定的字符串,比如:
ENTRYPOINT ["./greeting", "--message", "Hello, world!"]
world是环境变量的值,我该怎么做呢?我尝试使用“你好,$ADDRESSEE”,但这似乎不工作,因为它需要$ADDRESSEE字面上。
当前回答
我使用上面“创建自定义脚本”方法的变体解决了这个问题。是这样的:
FROM hairyhenderson/figlet
ENV GREETING="Hello"
RUN printf '#!/bin/sh\nfiglet -W \${GREETING} \$@\n' > /runme && chmod +x /runme
ENTRYPOINT ["/runme"]
CMD ["World"]
像
docker container run -it --rm -e GREETING="G'Day" dockerfornovices/figlet-greeter Alec
其他回答
我使用上面“创建自定义脚本”方法的变体解决了这个问题。是这样的:
FROM hairyhenderson/figlet
ENV GREETING="Hello"
RUN printf '#!/bin/sh\nfiglet -W \${GREETING} \$@\n' > /runme && chmod +x /runme
ENTRYPOINT ["/runme"]
CMD ["World"]
像
docker container run -it --rm -e GREETING="G'Day" dockerfornovices/figlet-greeter Alec
在我的案例中,工作方式是这样的:(用于docker中的Spring引导应用程序)
ENTRYPOINT java -DidMachine=${IDMACHINE} -jar my-app-name
并在docker运行时传递参数
docker run --env IDMACHINE=Idmachine -p 8383:8383 my-app-name
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\!"
以下是对我有效的方法:
ENTRYPOINT [ "/bin/bash", "-c", "source ~/.bashrc && ./entrypoint.sh ${@}", "--" ]
现在您可以向docker run命令提供任何参数,同时仍然可以读取所有环境变量。
我试图用建议的答案来解决问题,但仍然遇到了一些问题……
这是我问题的解决方案:
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"]