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

•

u/AutoModerator Dec 11 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

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.

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.

4

u/jestermax22 Dec 12 '22

It’s more important when you write apps that are in pools of 10’s or more of servers. You don’t want servers or configs to be “special” in any way or else you’ll spend more time investigating your infra during production issues.

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.

4

u/silverscrub Dec 12 '22

since the idea of a JVM should make the Java code run on any environment?

It is convenient that you can run your code on any environment, but you still want your code to run on the same environment because it's more reliable.

For example, maybe your program needs a database. You can either write in your readme how to install and configure the correct database for your program or you can include a Docker container.

Next, maybe one developer has some OS related issues when running the program locally. You could have this solved if your program was in a docker container.

It's really just at extension of a good principle to follow when coding. You want to describe a procedure as code instead of repeating yourself wherever that procedure is needed.

3

u/AsleepThought Dec 12 '22

Because Java does not run on ANY platform unless you install the JVM.

Then you need to ask "which version of the JVM should be installed on the system?". Unfortunately for you (the system admin) there are now a plethora of legacy apps dependent on old Java, and a plethora of new apps dependent on new Java.

And that is not counting other third-party software that also needs its own Java management.

As you can see from this, Java being able to "run in any environment" stopped being a real feature in the late 90's. Its simply not true, and arguably never was true.

Java's dependence on a JVM to run your programs makes it as limiting for real-world deployments as any other interpreter-bound programming language. Sad.

So make sure to Dockerize your Java app, or its likely no one will ever be able to actually use it unless you really put in the work to bundle Java installation and management methods into your app's installer.

2

u/Azifor Dec 12 '22

You may not need to. Depends on your use case for your app.

But if you want it easily available to the world, then a container just throws in the underlying infrastructure to ensure it does without you needing to coordinate that part.

2

u/ignotos Dec 12 '22

Another benefit is that it's simply very well supported with tooling. "Run this docker image on this server" is something which works with minimal fuss and configuration.

So you can run a simple command on your machine saying "package up my project into a docker image, and tag it as my-app version 1.0", and then on your cloud provider you can say "I want to spin up servers running 10 copies of my-app version 1.0".

Then, later on, you can make some changes to your code, package it up as version 1.1, and have all of your servers updated with a couple of clicks in the web console or command-line tool.

2

u/jeromejahnke Dec 12 '22

It is less about the environment your app runs in and more about the system your app runs in. So all the connections into and out of the machine are easier to manage with containers. Version dependencies for your app are easier with containers, where your workload is running is easier with containers. Think bigger when thinking about containers. I used to have systems that would run on 100 machines in 5 data centers. And making sure everything was connected and up to date took an enormous amount of time. But now it runs on more machines and in more data centers but takes a LOT less work cause Containers and Container Management.

2

u/loserIIITian Dec 12 '22

Hi, I use docker for development since we work in teams and we want to make sure everyone is on the same page regarding dependencies (versions mainly). Also we require container orchestration for deployment so we use docker and eks for our API (springboot rest api so an extension of java)