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

2019-01-23 12:27发布

问题:

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.

回答1:

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?



回答2:

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


回答3:

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"


回答4:

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