我正在嘗試基于構建時傳遞的 arg 啟動一個應用程式
指令:
docker build --build-arg profile=live . -t app
Dockerfile:
FROM openjdk:11.0.7-jre-slim-buster
WORKDIR /app
ARG JAR_FILE=target/*.jar
ARG profile
ENV profile ${profile:-dev}
EXPOSE 8080
COPY ${JAR_FILE} /app/app.jar
# ENTRYPOINT ["java", "-jar", "app.jar", "--spring.profiles.active=${profile}"] --- not working
RUN echo $profile <--- here I got the value
#CMD java -jar app.jar --spring.profiles.active=${profile} --- not working
#CMD java -jar app.jar --spring.profiles.active=$profile --- not working
CMD ["sh", "-c", "node server.js ${profile}"] --- not working
當我檢查 docker 影像時,我得到了
"Cmd": [
"sh",
"-c",
"node server.js ${profile}"
],
我缺少什么?
謝謝
更新:使用CMD java -jar app.jar --spring.profiles.active=$profile并且$profile在運行時將具有所需的值
uj5u.com熱心網友回復:
環境替換不會發生在CMD. 相反,它發生在容器內運行的 shell 中(在您的情況下sh,雖然不清楚您為什么使用 json/exec 語法來呼叫sh命令)。
有關環境替換的檔案可從以下位置獲得:https : //docs.docker.com/engine/reference/builder/#environment-replacement
uj5u.com熱心網友回復:
試試這個 Dockerfile: docker build --build-arg PROFILE=uat -t app .
FROM alpine
ARG PROFILE
ENV PROFILE ${PROFILE:-dev}
CMD ["ash", "-c", "while :; do echo $PROFILE; sleep 1; done"]
運行它,它每秒列印一次:docker run -it --rm app
當你說“不作業”時,你沒有提到結果到底是什么。假設您有空字串或其他意外值,環境變數可能會被基礎映像使用。為您的環境變數嘗試另一個名稱,或使用非精簡版的 openjdk 映像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/353060.html
