Search code examples
dockercmddockerfileenvironment-variables

Build argument with Dockerfile CMD


In my Dockerfile cmd command is like this,

CMD ["java", "-javaagent:dd-java-agent.jar", "-Ddd.env=my_env", "-Ddd.agent.host=172.17.0.2", "-jar", "app.jar"]

I want to make this dd.agent.host and dd.env values to be dynamic when building the images or run the containers.

Can anyone please let me know a way to do this without changing to CMD ["sh", "-c", "java..."] format.

When I changed to CMD ["sh", "-c", "java..."] and used ARG and ENV to do this and with that changes log4j2.xml file environment variables (${env:service-host}) not getting the value. Any idea on this?


Solution

  • After research on this problem I found that without setting -Ddd.agent.host and -Ddd.env in the CMD we can pass it in the docker run command as a environment variables. I have added following to the docker run command.

    -e DD_AGENT_HOST=172.17.0.2 -e DD_ENV=my_env