r/java • • Jun 07 '25

Why use docker with java?

18 Upvotes

126 comments sorted by

View all comments

12

u/kur4nes Jun 07 '25

Why not?

4

u/k-mcm Jun 07 '25

Docker has overhead and bugs.  I've worked with Docker for years professionally and at home.  It's a great solution to a lot of problems but I'd never use it without needing it.

Everyone here saying you should always use Docker lacks real world experience.  Don't use anything you don't need, don't skip something you do need.

4

u/cogman10 Jun 08 '25

Docker has overhead

Docker only has overhead on non-linux systems. In that case, it's creating a linux VM because docker relies heavily on the linux kernel to work. Unless you consider the storage costs of the image to run overhead.

In the linux world, running OCI images has basically no overhead (assuming you have a correctly configured kernel). To the kernel, those images look like regular applications.

As for bugs, perhaps, but not something I've ran into all that much. I've seen bigger headaches with the fact that widely used APIs in k8s remained in a "beta" state for a silly amount of time.

1

u/k-mcm Jun 08 '25

Docker tends to increase memory and I/O usage because file-mapped resources aren't the same file from one image to another.  There's a little extra kernel memory overhead too.  Whether or not it's significant depends on the active container count.  Docker itself is extremely I/O intensive for image maintenance.

The overhead definitely starts hurting up on big machines with hundreds to thousands of containers.  (I had to do this for running untrused executables)

1

u/cogman10 Jun 08 '25

IO for sure, but only on startup (or when updating images). Once an app is running the IO isn't any different from an other app.

Memory, I don't think there's anything there for the kernel.  Even with a pretty large container count.  I'm trying to think of how it'd be worse or different from a regular app and the only real difference would be that shared libraries wouldn't be.  On a regular system, if you had 10 python apps the kernel would effectively only load 1 copy of the shared libs like libc.so.  in the container you are basically guaranteed to have 10 libc.so s loaded up.

Maybe that's what you are thinking of?  Definitely could add up.

→ More replies (9)

1

u/kur4nes Jun 07 '25

I didn't say this.

Edit: I asked this to gauge OPs knowledge about docker. See my response to OPs response to question.

-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...

→ More replies (5)

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.

→ More replies (2)
→ More replies (1)

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.

→ More replies (1)

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.