限制Docker容器中的JVM内存消耗 [英] Limit JVM memory consumption in a Docker container

查看:2314
本文介绍了限制Docker容器中的JVM内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot应用程序实现了一个我想在Docker容器中运行的服务。我遵循官方 Spring文档的指导原则,建议创建与此类似的DockerFile:

I've got a Spring Boot application implementing a service which I want to run in a Docker container. I've followed the guideline of the official Spring docs which suggest to create a DockerFile similar to this:

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

然后将图像推送到Docker后,我使用 Docker Compose 以这种方式启动它:

Then once the image is pushed to Docker I use Docker Compose to launch it this way:

  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'

这里我有 JAVA_OPTS vari但是,当我执行 docker stats spring-boot-docker 时,容器占用的内存过多(我理解了所占用的内存总量) JVM可能远远超过64M,但在这种情况下是完全无限的。)

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).

我也尝试过使用 mem_limit param ,但这会减慢显着降低了应用程序。

I've also tried with the mem_limit param, but this slows down the application noticeably.

推荐答案

经过一段时间的努力,似乎 JAVA_OPTS 变量可以传递给容器当它基于Tomcat映像时,但Spring Boot使用Java本身作为基本形象。

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.

我发现本教程解决了问题,只需修改DockerFile中启动进程的方式,并直接在ENTRYPOINT中添加JAVA_OPTS变量:

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:

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

这样,JVM将从命令本身中选择值。

This way, the JVM will pick the value from the command itself.

这篇关于限制Docker容器中的JVM内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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