如果我设置了一个环境变量,比如ENV ADDRESSEE=world,我想在入口点脚本中使用它,连接成一个固定的字符串,比如:
ENTRYPOINT ["./greeting", "--message", "Hello, world!"]
world是环境变量的值,我该怎么做呢?我尝试使用“你好,$ADDRESSEE”,但这似乎不工作,因为它需要$ADDRESSEE字面上。
如果我设置了一个环境变量,比如ENV ADDRESSEE=world,我想在入口点脚本中使用它,连接成一个固定的字符串,比如:
ENTRYPOINT ["./greeting", "--message", "Hello, world!"]
world是环境变量的值,我该怎么做呢?我尝试使用“你好,$ADDRESSEE”,但这似乎不工作,因为它需要$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中使用的变量必须是ENV类型(而不是ARG类型)。
例# 1:
ARG APP_NAME=app.jar # $APP_NAME can be ARG or ENV type.
ENV APP_PATH=app-directory/$APP_NAME # $APP_PATH must be ENV type.
ENTRYPOINT java -jar $APP_PATH
这将导致执行: Java -jar app-directory/app.jar
例2(你的问题):
ARG ADDRESSEE="world" # $ADDRESSEE can be ARG or ENV type.
ENV MESSAGE="Hello, $ADDRESSEE!" # $MESSAGE must be ENV type.
ENTRYPOINT ./greeting --message $MESSAGE
这将导致执行: 你好,世界!
请确认,在分配字符串变量时是否需要引号""。
我的建议:尽可能使用ENV而不是ARG,以避免您的部分或SHELL方面的混淆。
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"]
在经历了很多痛苦和@vitr等人的大力帮助后,我决定尝试一下
标准bash替换 ENTRYPOINT的外壳形式(上面的伟大提示)
这很有效。
ENV LISTEN_PORT=""
ENTRYPOINT java -cp "app:app/lib/*" hello.Application --server.port=${LISTEN_PORT:-80}
如。
docker run --rm -p 8080:8080 -d --env LISTEN_PORT=8080 my-image
and
docker run --rm -p 8080:80 -d my-image
两者都在容器中正确地设置了端口
Refs
见https://www.cyberciti.biz/tips/bash -壳参数替换- 2. - html
在我的案例中,工作方式是这样的:(用于docker中的Spring引导应用程序)
ENTRYPOINT java -DidMachine=${IDMACHINE} -jar my-app-name
并在docker运行时传递参数
docker run --env IDMACHINE=Idmachine -p 8383:8383 my-app-name