问题
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="myname@test.com"
# 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"
回答1:
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
回答2:
After looking at the error more closesly, i found the solution. Posting here, if someone needs in future.
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /my.jar"]
来源:https://stackoverflow.com/questions/53785577/passing-java-opts-to-spring-boot-application-through-docker-compose