r/learnjava • • Dec 11 '22

Why containerize a Java program using Docker?

I'm learning Docker and the idea of having a container for a Java app seems redundant to me since the idea of a JVM should make the Java code run on any environment?

But maybe I'm missing something and see if others containerize their Java apps using Docker and why.

46 Upvotes

15 comments sorted by

View all comments

59

u/ohlaph Dec 12 '22

What if I use Java 19, Bob uses Java 17, you use Java 11 and the project uses Java 11? If I want to run your project, it might not work, so I have to go find Java 11 first. But with a container, it pulls the same Java version for everyone and solves that issue.

8

u/CodeyGrammar Dec 12 '22

Good point, so it also handles containing multiple versions of Java in the same server for each distinct use case compared to installing multiple versions of Java within the same server.

4

u/ohlaph Dec 12 '22

Exactly, and this goes for other things you might be interested in, like if you add a database, or other components, the container handles all of that so each person or server using it doesn't have to be manually configured. I also just started using Docker, and playing with Docker Compose, and I have two computers I split my time on and it's so much better not having to spend time configuring each computer.