skip to Main Content

Here is a snippet of my Dockerfile:

ENV LOG_AS_JSON=false

ENV JAVA_OPTS="-Djson.logging=$LOG_AS_JSON"

When I try to pass the variable with the following command it is still set to the default value ‘false’:

docker run --detach --publish 8080:8080 --env LOG_AS_JSON=true --name app-json some_name:latest

2

Answers


  1. You would have to explicitly overwrite the JAVA_OPTS variable:

    docker run --detach --publish 8080:8080 --env JAVA_OPTS="-Djson.logging=true" --name app-json some_name:latest
    
    Login or Signup to reply.
  2. You first issue is that the expression is already evaluated at build time. So what’s in the image is

    ENV JAVA_OPTS="-Djson.logging=false"
    

    To prevent the build-time evaluation, you can use single quotes, so the statement becomes

    ENV JAVA_OPTS='-Djson.logging=$LOG_AS_JSON'
    

    Then, to get the container to actually replace the variable, you can use the eval command. Here I set a new variable, ‘foo’, to echo the contents of the variable and then eval the new variable and it prints out -Djson.logging=true.

    docker run --rm -e LOG_AS_JSON=true <image name> /bin/sh -c 'foo="echo $JAVA_OPTS"; eval $foo'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search