Getting started with Java development on Docker
In our example we will leverage Maven and Docker’s layered file system to install everything from scratch and at the same time have small turn around times when recompiling changes.
So the prerequisites you need: no Java, no Maven, just Docker. Crazy, eh? ;-)
The source and config Files
- The Maven config: pom.xml
- A Java class: Hello.java
- The Dockerfile
For the rest we will go into some detail on the structure of these three files below.
* SparkTutorials note: We cloned the repo and attached it as a ZIP below so it will always be available:
Setting up your POM file
If we now rebuild the app without any changes to the pom.xml, the previous steps are cached and only the last steps are run. This makes turnaround times much faster.
Building and running
$ docker build -t giantswarm/sparkexample .
Note that this will take a while when you start it for the first time since it downloads and installs Maven and downloads all the project’s dependencies. Every subsequent start of this build will only take a few seconds, as again everything will be already cached.
Once the image is built, start it with:
$ docker run -d -p 4567:4567 giantswarm/sparkexample
And test it with:
$ curl localhost:4567
hello from sparkjava.com
Now go ahead and change something in the source (like returning your own message) and rebuild this.... Isn't it great!?