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

28

u/_Atomfinger_ Dec 11 '22

The main reason is that they're easy to deploy and mostly immutable.

It is less about the JVM and Java code and more about infrastructure, ease of scale and configuration. Sure, Java can run in any environment, but most need Java installed, and maybe you need some configuration files, you need to configure firewalls and the network, etc...

With a container, you get to replicate those parts that would otherwise need a lot of scripting or manual tweaking. Yes, Java can, in theory, run anywhere, but containers aren't really tackling that problem - at least not concerning Java.

5

u/CodeyGrammar Dec 11 '22

I see, so it's more of a side effect that because of the benefits of containerizing the rest of the environment, Java should also be containerized within that same scope/boundary too. I think that makes sense.

1

u/Solonotix Dec 12 '22

The simple tag-line for developing with containers: Develop where you Build. Build where you Run.

Docker allows you to isolate your development, then it can be run through a language-agnostic pipeline that lands it in a language-agnostic host that orchestrates your containers. Inside the containers, magic happens for all anyone else cares. What's better, the nature of using Docker is automating the process from initial container to a fully-realized application.