简体   繁体   English

通过 docker-compose 将 JAVA_OPTS 传递给 spring 引导应用程序

[英]Passing JAVA_OPTS to spring boot application through docker-compose

I am creating a docker image using below configuration.我正在使用以下配置创建一个 docker 图像。 Once image is ready i want to pass JAVA_OPTS to my docker container, so it can be passed to my spring boot application.图像准备好后,我想将 JAVA_OPTS 传递到我的 docker 容器,这样它就可以传递到我的 spring 启动应用程序。 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.每当我尝试启动容器时,我都会收到“运行时创建失败:container_linux.go:348:启动容器进程导致”exec:\“java $JAVA_OPTS\”:$PATH 中找不到可执行文件“:未知”错误。 Am i missing something?我错过了什么吗? Any help is really appreciated非常感谢任何帮助

Dockerfile 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 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"         

You shouldn't use java $JAVA_OPTS with ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS"]您不应该将java $JAVA_OPTSENTRYPOINT ["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)它的主要问题是,使用这种方法,您的应用程序将不会收到 sigterm,因此在正常关闭的情况下,它对您不起作用(如果您不知道这一点,您会在此处找到有关该问题的更多信息)

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", ...]如果您想在 docker 环境中自定义 java 选项,请使用JAVA_TOOL_OPTIONS环境属性 ( https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/envvars002.html ) 和ENTRYPOINT ["java", ...]

With this property you can declare your expected options even in Dockerfile like:使用此属性,您甚至可以在 Dockerfile 中声明您期望的选项,例如:

ENV JAVA_TOOL_OPTIONS "-XX:MaxRAMPercentage=80"

And you can easily override it later with external provided docker or kubernetes property.您可以稍后使用外部提供的 docker 或 kubernetes 属性轻松覆盖它。

The JAVA_TOOL_OPTIONS is used by the jib project - more here JAVA_TOOL_OPTIONSjib项目使用 - 此处有更多信息

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"]

As some comments have pointed out ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS] prevents the container from shutting down cleanly. The solution is to use exec (an explanation is available there https://hynek.me/articles/docker-signals/ ): ENTRYPOINT ["sh", "-c", "exec java $JAVA_OPTS]正如一些评论所指出的ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS]阻止容器干净地关闭。解决方案是使用 exec(那里有一个解释https://hynek.me/文章/docker-signals/ ): ENTRYPOINT ["sh", "-c", "exec java $JAVA_OPTS]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM