简体   繁体   English

限制Docker容器中的JVM内存消耗

[英]Limit JVM memory consumption in a Docker container

I've got a Spring Boot application implementing a service which I want to run in a Docker container. 我有一个Spring Boot应用程序实现了一个我想在Docker容器中运行的服务。 I've followed the guideline of the official Spring docs which suggest to create a DockerFile similar to this: 我遵循官方Spring文档的指导原则,建议创建类似于此的DockerFile:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Then once the image is pushed to Docker I use Docker Compose to launch it this way: 然后一旦图像被推送到Docker我使用Docker Compose以这种方式启动它:

  spring-boot-docker:
    ports:
    - "80:80"
    expose:
    - "80"
    image: my-repo/spring-boot-docker:0.1.0-SNAPSHOT
    container_name: spring-boot-docker
    environment:
      JAVA_OPTS: '-Xmx64m'

Here I've got the JAVA_OPTS variable which limits the memory allocation, however, when I execute docker stats spring-boot-docker , the memory taken by the container is excessive (I understand the total memory taken by the JVM might be much more than 64M, but in this case is totally boundless). 这里我有JAVA_OPTS变量来限制内存分配,但是,当我执行docker stats spring-boot-docker ,容器占用的内存过多(我知道JVM占用的总内存可能远远超过64M,但在这种情况下是完全无限的)。

I've also tried with the mem_limit param , but this slows down the application noticeably. 我也尝试过mem_limit参数 ,但这会显着降低应用程序的速度。

After struggling for a while, it seems the JAVA_OPTS variable can be passed to the container when it's based in a Tomcat image , but Spring Boot uses Java itself as the base image. 经过一段时间的努力,似乎JAVA_OPTS变量可以在基于Tomcat映像时传递给容器,但Spring Boot使用Java本身作为基本映像。

I've found out this tutorial which solved the problem for me, just modifying the way the process is launched in the DockerFile and adding a JAVA_OPTS variable directly in the ENTRYPOINT: 我发现本教程解决了我的问题,只是修改了DockerFile中启动进程的方式,并直接在ENTRYPOINT中添加了一个JAVA_OPTS变量:

ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar

This way, the JVM will pick the value from the command itself. 这样,JVM将从命令本身中选择值。

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

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