Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing JAVA_OPTS to spring boot application through docker-compose

I am creating a docker image using below configuration. Once image is ready i want to pass JAVA_OPTS to my docker container, so it can be passed to my spring boot application. Whenever i try to bring up the container i am getting "runtime create failed: container_linux.go:348: starting container process caused "exec: \"java $JAVA_OPTS\": executable file not found in $PATH": unknown" error. Am i missing something ? Any help is really appreciated

Dockerfile

FROM openjdk:8-jdk-alpine

LABEL maintainer="[email protected]"

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8080 available to the world outside this container
EXPOSE 8080

# The application's jar file
ARG JAR_FILE=target/my.jar

# Add the application's jar to the container
ADD ${JAR_FILE} my.jar

ENV JAVA_OPTS=""
# Run the jar file 
ENTRYPOINT ["java $JAVA_OPTS","-Djava.security.egd=file:/dev/./urandom","-jar","/my.jar"]

docker-compose

version: '2.1'
services:
  service1:
    hostname: test
    domainname: mydomain.com
    image: myimage:latest
    container_name: test-container
    environment:
      - JAVA_OPTS=-Dapp.clients.scheme=http -Dapp.clients.port=9096 -Dserver.port=8082
    ports:
      - "8082:8082"         
like image 924
user2701108 Avatar asked Nov 30 '22 14:11

user2701108


1 Answers

You shouldn't use java $JAVA_OPTS with ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS"]

The main problem with it is that with this approach you application won't receive the sigterm so in case of graceful shutdown it won't work for you (you will find more about the problem here if you are not aware about that)

If you want customize the java opts on docker environments use JAVA_TOOL_OPTIONS environment property (https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/envvars002.html) and ENTRYPOINT ["java", ...]

With this property you can declare your expected options even in Dockerfile like:

ENV JAVA_TOOL_OPTIONS "-XX:MaxRAMPercentage=80"

And you can easily override it later with external provided docker or kubernetes property.

The JAVA_TOOL_OPTIONS is used by the jib project - more here

like image 58
Przemek Nowak Avatar answered Jan 07 '23 10:01

Przemek Nowak