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

后端 未结 5 441
囚心锁ツ
囚心锁ツ 2020-12-05 06:56

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 say

相关标签:
5条回答
  • 2020-12-05 07:10

    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?

    0 讨论(0)
  • 2020-12-05 07:18

    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"
    
    0 讨论(0)
  • 2020-12-05 07:21

    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
    
    0 讨论(0)
  • 2020-12-05 07:22

    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
    
    0 讨论(0)
  • 2020-12-05 07:30

    you can do it by specifying java options environment in docker compose file

    env: 
      - name: _JAVA_OPTIONS
        value: "-Xmx1g"
    

    it will change the heap size.

    0 讨论(0)
提交回复
热议问题