Docker Compose JVM parameters

2020-03-29 19:41发布

问题:

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

回答1:

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
...


回答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.