3

I wrote a java application that takes an environment variable that takes an argument to set a key for a JWT token salt key. Is there a way for me to pass the command variables in Docker Compose?

java -Djava.security.egd=file:/dev/./urandom -jar /user-profile-api.jar --key=blah

And to run the docker image you just

docker run -p 8080:8080 docker_image --key=blah
4
  • 1
    So you already can run the image like that? You can just pass the --key=blah bit as the command attribute for the service in the compose file. Commented Jul 18, 2016 at 21:59
  • That worked! Thanks! Mind adding it as an answer and I will add select it? Commented Jul 19, 2016 at 2:02
  • Glad it worked, done :) Commented Jul 19, 2016 at 2:45
  • @JoelHolmes, is the java command above located in the entrypoint? Commented Aug 7, 2020 at 9:29

2 Answers 2

2

If you already are able to run your docker container using:

docker run -p 8080:8080 docker_image --key=blah

Then you just need to override the command attribute for your service in the compose file to --key=blah. So:

services:
  app:
    command: --key=blah
...
Sign up to request clarification or add additional context in comments.

1 Comment

Please note: passing system arguments only works if you have exec form entrypoint (e.g.: ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]). Details are described here
2

One way would be to put your java command in a shell script (say, bootstrap.sh), and set that as your command to run in docker compose. And then in bootstrap.sh inject the key via an environment variable which is in your docker-compose.yml.

E.g.

bootstrap.sh

java -Djava.security.egd=file:/dev/./urandom -jar /user-profile-api.jar --key=$SALT_KEY

docker-compose.yml

build: .
environment:
    - SALT_KEY=blah
command: /opt/app/bootstrap.sh

Obviously you'd need to package up bootstrap.sh into your container for this to work.

2 Comments

This looks like it would work, but is there another way to be more flexible?
maybe, but flexible is a vague term, can you say exactly what flexibility you'd like to have?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.