通过docker-compose将JAVA_OPTS传递到Spring Boot应用程序 [英] Passing JAVA_OPTS to spring boot application through docker-compose

查看:1047
本文介绍了通过docker-compose将JAVA_OPTS传递到Spring Boot应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下配置创建docker映像.映像准备好后,我想将JAVA_OPTS传递给我的docker容器,以便可以将其传递给我的spring boot应用程序.每当我尝试启动容器时,我都会收到运行时创建失败:container_linux.go:348:启动容器进程导致"exec:\" java $ JAVA_OPTS \":在$ PATH中找不到可执行文件":未知"错误.我想念什么吗?任何帮助都非常感激

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

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"         

推荐答案

您不应将java $JAVA_OPTSENTRYPOINT ["sh", "-c", "java $JAVA_OPTS"]

主要问题在于,使用这种方法,您的应用程序将不会收到sigterm,因此,在正常关机的情况下,它对您不起作用(您将找到有关该问题的更多信息

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)

如果要自定义docker环境上的java opt,请使用JAVA_TOOL_OPTIONS环境属性(

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

使用此属性,您甚至可以在Dockerfile中声明期望的选项,例如:

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

ENV JAVA_TOOL_OPTIONS "-XX:MaxRAMPercentage=80"

然后您可以使用外部提供的docker或kubernetes属性轻松地覆盖它.

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

JAVA_TOOL_OPTIONSjib项目使用-更多这里

这篇关于通过docker-compose将JAVA_OPTS传递到Spring Boot应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆