Why is it faster to start a jar in a Docker-Container than on local machine? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Why is it faster to start a jar in a Docker-Container than on local machine? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Currently I’m testing with Microservices and Docker-Container. And during my last try with a Micronaut-Server I saw differences between the start-up-time for starting local (cmd) and starting with Docker. But what made me curios, is the fact that the Container was much faster.

I’m creating a runnable jar (more precisely a shadowjar – not sure what the exact difference is) with Gradle. Then I build a Docker-Image with that jar file. The start command for both is the same (see the Dockerfile below): java -jar micronaut.jar

During my search for a reason for that I found this question which is also about performance of Docker-Container, but the conclusion was more, that the Container should be slightly slower, not faster.

My Dockerfile:

FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
COPY build/libs/*.jar micronaut.jar
EXPOSE 8080
CMD java -jar micronaut.jar

and the docker command: docker run -p 9999:9999 -it --name dokuserver pge/dokuserver:0.1

I expected that the start-up-time would be the same oder a bit slower for the container but actually the time is.

  • Local: 4000-5000ms
  • Docker: ~2500ms

I tried several times but the outcome ist always nearly the same.

I’m working on a Win10 PC with DockerDesktop (Docker 19.03.1), IntelliJ and Gradle (5.5.1) and used the IntelliJ-Terminal and the windows-cmd for the local start.

I’m no an expert in Docker or the things which happens closer to the hardware so I couldn’t find an answer for this speed difference. So I’m asking you:

What could cause that?

Answer

AdoptOpenJDK has builds with two different JVMs: HotSpot and OpenJ9

HotSpot and OpenJ9 are totally different implementations of JVM with different JIT compilers, GC algorithms and internal architecture.

As your docker file suggests, you are using adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim which is a name suggests OpenJ9 based.

On Windows your are likely to use HotSpot based JVM (java -version to know for sure).

OpenJ9 has less aggressive compiler optimizations so difference in start up time is not surprising.

We are here to answer your question about Why is it faster to start a jar in a Docker-Container than on local machine? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji