Home > OS >  Dcokerfile pass ENV to ENTRYPOINT or CMD
Dcokerfile pass ENV to ENTRYPOINT or CMD

Time:11-09

I'm trying to start an app based on arg passed at build time

cmd: 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

when I inspect the docker image I got

"Cmd": [
            "sh",
            "-c",
            "node server.js ${profile}"
        ],

What i'm missing?

Thanks

update: works with CMD java -jar app.jar --spring.profiles.active=$profile and the $profile will have the desired value at runtime

CodePudding user response:

Environment replacement doesn't happen in CMD. Instead it happens in the shell running inside the container (in your case sh, though it's not clear why you've used the json/exec syntax to call a sh command).

Documentation on environment replacement is available from: https://docs.docker.com/engine/reference/builder/#environment-replacement

CodePudding user response:

Try this 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"]

Run it and it prints uat every second: docker run -it --rm app

You didn't mention what exactly is the outcome when you said "not working". Presumed you got empty string or other unexpected value, the environment variable could be in used by the base image. Try another name for your environment variable, or use non-slim version of openjdk image.

  • Related