1

I am trying to deploy several Java (spring boot) apps in docker containers in 1 host, where I set memory limits (--memory=30m --memory-swap=50m) for each.

However when I check the limits using docker container stats, I see each container is using >400MB of the host's RAM. Due to this I cannot start all the containers I need as the kernel kills some of them (OOM).

What do I need to do to ensure that the containers' memory is controlled using the docker memory options?

My host is a digital ocean centos 7. Thanks

Kimutai
  • 1,022
  • 11
  • 15

1 Answers1

0

Main reason for this issue is that JRE is not aware it is running inside a container.

Let JVM detect how much memory is available in Docker container https://blog.csanchez.org/2017/05/31/running-a-jvm-in-a-container-without-getting-killed

JAVA_OPTS="-server -Djava.net.preferIPv4Stack=true -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1"

Make sure you have JDK 8-131 or above version
In case of JDK-9 it will be available to correctly detect available memory inside container.

Additional Referece: Docker run -m doesnt set the limit (JVM takes up the entire host machine's size for xms and xmx)
and
https://developers.redhat.com/blog/2017/03/14/java-inside-docker/

fly2matrix
  • 2,351
  • 12
  • 13
  • Thanks for your answer. About the JAVA_OPTS, should I put them in the Dockerfile or on the docker run command. Please a sample Dockerfile or docker run command if necessary – Kimutai Oct 13 '18 at 08:35
  • Use JAVA_OPTS as ENV in Dockerfile and Override if required while creating container from the image. – fly2matrix Oct 15 '18 at 10:31
  • it is actually java 10 that introduce container awareness (as well as a useful flag to deal with it: `-XX:MaxRAMPercentage`). Try with: `docker run -m 1GB openjdk:10-jre java -XshowSettings:vm -version` – ThanksForAllTheFish Oct 30 '18 at 12:33