如何在 Docker 容器内设置 Java 堆大小(Xms/Xmx)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29923531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Java heap size (Xms/Xmx) inside Docker container?
提问by Tung Nguyen
As of raising this question, Docker looks to be new enough to not have answers to this question on the net. The only place I found is this articlein which the author is saying it is hard, and that's it.
在提出这个问题时,Docker 看起来足够新,无法在网上回答这个问题。我发现的唯一地方是这篇文章,作者说这很难,仅此而已。
采纳答案by jbarrueta
I agree that it depends on what container you're using. If you are using the official Tomcat image, it looks like it's simple enough, you will need to pass the JAVA_OPTS
environment variable with your heap settings:
我同意这取决于您使用的容器。如果您使用的是官方Tomcat 镜像,看起来很简单,您需要JAVA_OPTS
使用堆设置传递环境变量:
docker run --rm -e JAVA_OPTS='-Xmx1g' tomcat
See How to set JVM parameters?
请参阅如何设置 JVM 参数?
回答by Mark O'Connor
It all depends how your Java application is packaged and how it's configuration files are exposed using Docker.
这完全取决于您的 Java 应用程序是如何打包的,以及它的配置文件是如何使用 Docker 公开的。
For example the official tomcat imagestates that the configuration file is available in the default location: /usr/local/tomcat/conf/
例如官方的tomcat镜像声明配置文件在默认位置可用:/usr/local/tomcat/conf/
So easy to override entire directory or just one configuration file:
很容易覆盖整个目录或仅一个配置文件:
docker run -it --rm -p 8080:8080 -v $PWD/catalina.properties:/usr/local/tomcat/conf/catalina.properties tomcat:8.0
回答by occasl
You can also just place those settings in your image so something like the following would exist in your Dockerfile:
您也可以将这些设置放在您的图像中,这样您的 Dockerfile 中就会存在以下内容:
ENV JAVA_OPTS="-XX:PermSize=1024m -XX:MaxPermSize=512m"
回答by hawkeye
Note that in a docker-compose.yml
file - you'll need to leave out the double-quotes:
请注意,在docker-compose.yml
文件中 - 您需要省略双引号:
environment:
- JVM_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m
or
或者
environment:
- CATALINA_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m
回答by Riswan chatholi
you can do it by specifying java options environment in docker compose file
您可以通过在 docker compose 文件中指定 java 选项环境来实现
env:
- name: _JAVA_OPTIONS
value: "-Xmx1g"
it will change the heap size.
它会改变堆大小。