Search code examples
dockercloud-foundryswisscomdev

User-provided environment variable within docker CMD


I have successfully pushed my docker image to the swisscom app cloud (similar to this example: https://ict.swisscom.ch/2016/05/docker-and-cloudfoundry/).

Now I would like to use a user-provided environment variable within my docker CMD. Something like this:

ADD target/app.jar app.jar
CMD java -jar app.jar -S $USER_PROVIDED_ENV_VARIABLE

I also tried system-provided environment variables:

ADD target/app.jar app.jar
CMD java -jar app.jar -S $VCAP_APPLICATION

What am I doing wrong here?


Solution

  • If your Dockerfile is built like that, you'll simply need to pass the -e flag when running the image.

    Example Dockerfile:

    FROM ubuntu:16.10
    ENV MY_VAR "default value" # Optional - set a default value.
    CMD echo $MY_VAR
    

    Build the image:

    docker build -t my_image .
    

    Run a container from the image:

    docker run -e MY_VAR="my value here" my_image