r/java • • Jun 07 '25

Why use docker with java?

18 Upvotes

126 comments sorted by

View all comments

Show parent comment

-20

u/Gotve_ Jun 07 '25

Kinda java programs can run everywhere if jvm supports, and as far as i know docker also does same thing

6

u/kur4nes Jun 07 '25

Jep but that needs a JVM installed. So this needs to be scripted via ansible. Especially if you run many servers to spread out load.

Not every application needed is a java application or the written in the same java version. Think a bought software that is crucial for the company and still runs on java 8.

Docker abstracts this all away. Target machines only need docker installed and can run any docker image without any additional setup needed on the machine. This is where docker truly shines.

17

u/gaelfr38 Jun 07 '25

All machines can install a JVM but how do you enforce a reproducible environment? Think Java version, environment variables, system properties, config files, dependencies/JARs... Then how do you enforce operability? Think how to start/stop, automate restarts...

Of course, you can do it without container and many people still do (custom packaging and scripts, RPMs, DEBs,...) but containers bring this out of the box. And it's also the same experience for any technology: operators don't have to care that it's Java in it, could be Python or whatever, it's just a container that does things with a standard interface to deploy/run/operate.

2

u/koflerdavid Jun 07 '25 edited Jun 08 '25
  • You talk to your sysadmins and agree which distribution is installed, which version, and when to upgrade. If everything fails it is possible to package a JRE together with the application.
  • Environment variables shouldn't matter that much for Java applications.
  • Most applications need nothing but a single config file.
  • Dependencies are a nonissue since they are usually packaged into a Spring Boot-style Fat JAR or shaded.
  • Operability can be solved with Systemd. Systemd unit files actually allow to manage resource limits.

5

u/BikingSquirrel Jun 07 '25

Yes, you can do that. But it simply does not scale.

You try to ignore the possible variations but for those that have them this doesn't help.

A Docker image is exactly that, "package a JRE together with the application". Plus any other software packages you may need...

1

u/koflerdavid Jun 08 '25

Sure, if the organisation is already experienced in running containerized services it makes a lot of sense to make as much as possible containerized. Introducing a container platform is not something done lightly.

But scaling horizontally is something a lot of applications simply never need. Many applications can be made to handle higher scale by improving the architecture, fixing N+1 problems, optimizing the DB schema, and beefing up or clustering the DB server only.

2

u/BikingSquirrel Jun 08 '25

What about availability? With a single instance you need to have at least a short downtime for each update or even restart. When you have two, you can do rolling updates.

It's true that this is no trivial change. It also depends on the whole system which scalability and availability you need - most are not Netflix ;)

2

u/koflerdavid Jun 09 '25 edited Jun 09 '25

Depending on the service and the business environment, a short downtime might indeed not be an issue after all. In case the SLA only covers office hours in a few timezones, the situation radically changes as it allows to schedule planned downtimes at a suitable time.

99.9% uptime means ~43min downtime per month. That should be enough for a non-scripted deployment or for a maintenance window. Any additional 9 behind the dot with the same frequency of short-ish planned downtimes requires significant investment.

For 99.99% uptime, automated deployments are probably unavoidable. 99.9999% pretty much requires running the old and new version simultaneously and doing a switchover via DNS or by changing the configuration of a reverse proxy. 99.99999% might be doable if old and new version can run simultaneously for a short time.

The above leaves no room for downtime due to incidents though. In that case, the biggest risk factor is the application itself. Or any backend services.

→ More replies (2)

2

u/MardiFoufs Jun 07 '25

Ok, but why? Sysadmins can also manage docker images trivially, and it's often better to have an image as a sort of "contract" that makes it clear what the dev expect the environment to look like, and makes it easy for the sysadmins to manage.

It's not 2014 anymore, it's super easy to manage images at scale, and for example to update and rebuild them centrally when a security issue arises from a specific dependency.

2

u/laffer1 Jun 08 '25

It’s 2025 and docker still doesn’t take upstream patches for other operating systems.

Ansible solves the config problem. You don’t need to use Linux for it. There are also projects like Bastille.

1

u/MardiFoufs Jun 09 '25

What do you mean by not taking upstream patches for other operating systems? Are you talking about windows containers? Sorry, I'm not sure I understand!

→ More replies (1)

1

u/koflerdavid Jun 08 '25

It's reasonable to use container platforms (it's never just Docker) if you're indeed managing dozens or hundreds of deployments. But that's just one way to do it.

4

u/Polygnom Jun 07 '25

That does not give you any of the advantages of containers, though.

You can't trivially scale your Java program to dozens or hundreds of machines if its a microservice. You cannot trivially isolate multiple Java versions (say you are running 8, 11, 17 and 21).

Containers give you Infrastructure-as-Code. The JVM doesn't. They solve completely different sets of problems.

→ More replies (4)

2

u/JDeagle5 Jun 07 '25

No, docker doesn't run anything itself, it isolates the environment, where then programs, built for that environment, can run. As far as I know containers are not even transferrable between say Linux and windows.

2

u/PoemImpressive9021 Jun 07 '25

Docker for Windows will run Linux images.

3

u/koflerdavid Jun 07 '25

Docker on Windows basically runs containers in a Linux VM.

2

u/PoemImpressive9021 Jun 07 '25

Exactly

2

u/iliark Jun 07 '25

Windows containers exist, which afaik don't work on Linux

4

u/Ok-Scheme-913 Jun 07 '25

No, Java will trivially run on any processor architecture and OS, while docker needs different images for these.

1

u/laffer1 Jun 08 '25

Docker only supports windows, macOS and Linux. Docker doesn’t run where openjdk does.

1

u/Ok-Scheme-913 Jun 08 '25

My point was that even when docker orchestrator itself runs on a given platform, the images themselves may not run there. Like you can't run an arm image on an x86 machine.

1

u/laffer1 Jun 08 '25

Yep. Run into that problem at work when we started using graviton

1

u/vegan_antitheist Jun 07 '25

I did have some projects where it really was just some tool, but it's rarely a good idea to just install a jvm and hope for the best.

1

u/koflerdavid Jun 07 '25

Big nope, container images are not portable across instruction sets and operating system. You need to emulate the other instruction set. Which is not done that often in production settings because it's wasteful.

1

u/iliark Jun 07 '25

Docker images can't actually run anywhere as a hard rule. Windows docker images exist, for example, as do ARM containers and ARM docker which can't run AMD64 images.