Java Dev Environments with Containers
So you just finished watching some of Josh Long's awesome Spring Boot talks and you felt compelled to visit start.spring.io to build your first application. Or perhaps Eclipse MicroProfile is your thing and you thought going to start.microprofile.io to have your first project created. Or even more ludicrous, and you want to go supersonic subatomic Java with Red Hat's Quarkus project.
But you forgot you just happen to be with this other computer and you don't have your development environment at hand. Or perhaps you want to try all these different frameworks, and you don't want to mess around with different versions of tools, CLIs, JDKs, and so on.
Fact is, development environments are not easy to set up and even more difficult to keep them tidy — I know how it is. For Java developers, it requires, at the very minimum, to install a JDK, an editor, and a build tool like Maven or Gradle.
Fortunately enough, you might only need to install Visual Studio Code (insiders, for now), the Remote Development extension pack, and Docker Desktop. With this combo, you will be able to run these frameworks on your computer, without having to install anything else.
Let me show you.
Once you got your VS Code with the Remote Development extension pack installed, go to start.spring.io and download a project that comes with the Web dependency. Extract the file take note of the location of the folder.
Once you got that, go and open the command palette and select Remote-Containers: Open Folder in Container… and select the folder you just extracted from the zip file. It will then ask you for a Docker image to use as your development environment. If you didn't change the Java version on the Spring Initializr website, you will need Java 8. After selecting the image Java 8, Visual Studio Code will reload the window, and start a container for you with a basic Java environment (JDK 8, Maven, and Gradle), and connect your folder within that.
Any required extension to work with Java projects, will be installed inside the Docker container that was just loaded to open this folder. Which extensions are installed come from the definition of the dev container.
Now you can safely modify the Java class to add a Rest Controller and then hit Run above the main method.
At this point, if you try to access http://localhost:8080, it will fail. Before you do that, you must forward the port from the devcontainer to your host computer.
Go back to the command palette and hit Remote-Containers: Forward Port from Container… and then select port 8080.
VS Code will give you a nice pop-up at the bottom with a link to open the URL in your browser. Voi lá! No need to install JDK nor Maven to build and run your Spring Boot application!
Now, the same should work just fine for vanilla Eclipse MicroProfile projects, which are mostly Maven based. By the way if you prefer Gradle, the predefined devcontainers come with Maven and Gradle installed.
With Quarkus though, the process is a bit different because to create a Quarkus application, you actually use Maven to scaffold a project from the Quarkus Archetype. This method also works with Micronaut, which comes with a specific CLI which you can find a quick instruction set further below.
Start a fresh window of Visual Studio Code with no folder open.
Go to the command palette and select again Remote-Containers: Open Folder in Container… but this time, create a new folder when the folder selection dialog opens, and select the new, empty folder.
Then it will ask you for which devcontainer image to use. Select Java 8 again.
The folder will only contain a subfolder named .devcontainer, which is used to hold the configuration and the Dockerfile used to spin up this dev environment. You may as well modify that if you want later on, and even commit to your version control system.
Open the Terminal (Mac: ^`) and create a new Quarkus project with the following Maven command:
mvn io.quarkus:quarkus-maven-plugin:0.14.0:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=getting-started \
-DclassName="org.acme.quickstart.GreetingResource" \
-Dpath="/hello"
You can now go into the folder getting-started
and start Quarkus with mvn package quarkus:dev
. Once the server is up and running, forward the port as instructed earlier, and hit your browser at http://localhost:8080/hello.
Finally, as a quick note for playing with Micronaut, the only thing you need to do is to install SDKMAN! and then Micronaut CLI in the terminal that you can open right after opening an empty folder in a devcontainer.
Once you do that, run first this APT command to get the required package for SDKMAN!: apt-get update && apt-get install zip
. Now you can install SDKMAN! and then Micronaut:
curl -s “https://get.sdkman.io" | bash \
&& source “/root/.sdkman/bin/sdkman-init.sh” \
&& sdk install micronaut
After that, just create a Micronaut application and run it:mn create-app example
. Follow the rest of the Micronaut guide for further instructions.
If you want go even further, you can actually connect to remote development environments over SSH running anywhere.
For further readings, follow these links:
- Remote Development with Visual Studio Code
- Definitions of devcontainers on GitHub
- Remote Development Extension Pack
Hit me on Twitter if you have questions, or comment below. And please leave an applause if you liked it. :-)