How to set Java heap size (Xms/Xmx) inside Docker

2019-01-23 12:26发布

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 article in which the author is saying it is hard, and that's it.

4条回答
你好瞎i
2楼-- · 2019-01-23 12:58

It all depends how your Java application is packaged and how it's configuration files are exposed using Docker.

For example the official tomcat image states that the configuration file is available in the default location: /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
查看更多
对你真心纯属浪费
3楼-- · 2019-01-23 13:03

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:

docker run --rm -e JAVA_OPTS='-Xmx1g' tomcat

See How to set JVM parameters?

查看更多
不美不萌又怎样
4楼-- · 2019-01-23 13:14

Note that in a docker-compose.yml file - you'll need to leave out the double-quotes:

  environment:
  - JVM_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m

or

  environment:
  - CATALINA_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m
查看更多
戒情不戒烟
5楼-- · 2019-01-23 13:19

You can also just place those settings in your image so something like the following would exist in your Dockerfile:

ENV JAVA_OPTS="-XX:PermSize=1024m -XX:MaxPermSize=512m"
查看更多
登录 后发表回答