In my previous article I’ve been talking about using Java 10 to build and run your Spring Boot app. Now it is time to talk how to put it in Docker container. Better support of containerization was one of the main features of Java 10 release. So, let’s go!
That is fairly simple process, so just take a look on following code snippet:
Here, as you can see, I use multi-stage Docker build file. Quick note: multi-stage builds are supported with Docker 17.05 or higher but result image is backward compatible(at least from my experience). On the first stage it downloads JDK from Oracle’s, but it can make sense to store your own copy somewhere closer to your build server since it isn’t that small — a little bit less then 200mb. So, it is downloaded, unziped and ready to be used. But not so quick.
As you probably remember, Java 9 gave us module system and JDK itself is also modularized. That means we don’t have to bring whole JDK to production but we could build our own striped version specifically for our app’s needs. There is jlink tool provided for that. Just take a look how it is used in the build process. The modules listed there should be enough to run a typical Spring Boot application. In my production case I had to put java.scripting, jdk.scripting.nashorn to the list as well.
On the second stage it copies results from previous one, defines some environment variables, exposes port, copy launch.sh and app’s artifact. Not a rocket science at all.
And here is launch.sh script I’ve just mentioned:
That is just a small helper to ensure that there are all important things provided, etc. But, sure, it is optional.
And that should be enough to put your app to the production. Please, put some comments down if you have some thoughts how to make it better!