Home > Blockchain >  Unable to run docker container with Spring data source arguments
Unable to run docker container with Spring data source arguments

Time:12-07

I want to run a Docker container with some data source arguments the way I run a Spring Boot app on the terminal with Spring data source arguments. For example:

java  -Dserver.port=8999 -Dlogging.org.hibernate.SQL=DEBUG -Dlogging.level.ROOT=DEBUG -Dlogging.level.io.github.jhipster=DEBUG -Dlogging.level.com.opti.ecom=DEBUG -Dlogging.path=/var/log/spring/ecom_v2/ -jar target/LatestBuild/ecom-0.0.1-SNAPSHOT.jar --spring.jpa.show_sql=true --spring.profiles.active=dev,no-liquibase --spring.datasource.url=jdbc:postgresql://aws_database_url --spring.datasource.username=user --spring.datasource.password=password --spring.datasource.hikari.maximum-pool-size=10

I have tried with the docker run --env and the above individual args but it doesn't work.

I don't want to pass these args in the application.properties file.

Docker file:

FROM openjdk:11.0.7-jre-slim

ENV DEMO_ROOT=/root

ADD /target/LatestBuild/ecom-0.0.1-SNAPSHOT.jar $DEMO_ROOT
WORKDIR ${DEMO_ROOT}

CMD  ["java", "-jar", "ecom-0.0.1-SNAPSHOT.jar"]

Would be glad to get some help on this. Thanks!

CodePudding user response:

A good practice is to use a configuration file that contains theses configuration properties (application.properties)

You can use a Docker file like this:

FROM openjdk:11
VOLUME /conf
ADD application.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar --spring.config.location=file:/conf/" ]
EXPOSE 80
EXPOSE 81

In the /conf volume, you should copy your application.properties

CodePudding user response:

You can specify Spring properties as environment variables and particularly in a container setup this is much easier than trying to pass them as command-line arguments.

In a single docker run command you could write

docker run \
  -e SERVER_PORT=8999 \
  -e LOGGING_ORG_HIBERNATE_SQL=DEBUG \
  ...
  -e SPRING_DATASOURCE_HIKARI_MAXIMUM_POOL_SIZE=10 \
  myimage

Given the number of settings, you may find it more convenient to put them in a dedicated file which you can pass to the docker run --env-file option.

docker run \
  -d \
  -p 8999:8999 \
  --name myapp \
  --env-file spring.env \
  myimage
  • Related