Skip to content

Working with projects on Docker

Martin Lippert edited this page Dec 16, 2020 · 5 revisions

Working with docker in the boot dashboard

Spring Boot 2.3 introduced direct support for creating OCI container images when building Spring Boot projects. This support is now integrated in the Spring Boot Dashboard in the Spring Tools 4 for Eclipse.

Creating a docker target

The central piece of the integration is a new target type in the Spring Boot Dashboard in the Spring Tools 4 for Eclipse. This well-known dashboard for all your Spring Boot projects now integrates with Docker. Therefore you can create a new Docker target section the dashboard by specifying the path to the Docker daemon.

Creating images and running them

Once created (assuming you have Docker running on your machine), you can easily drag&drop your workspace projects onto that docker target. In case the projects use Spring Boot 2.3 (or higher), this drag&drop operation initiates a build of the project with the goal to create a container image using the Spring Boot support for creating OCI container images. The output of that build appears in a console, so that you can see exactly what is going on as part of the build.

By default, the initiated build uses the Maven wrapper that is included in the project to execute the build.

Once the build succeeded, the created image will show up as part of the application node in the docker section of the boot dashboard. Once the image is created, the dashboard will run that image automatically in a newly created container and show that running container as a child of the image node in the boot dashboard. In addition to the running container, the boot dashboard automatically exports the port of the running application in the container to the outside, so that you can easily access the application from your machine. The easiest way to do this in the boot dashboard is via a double click on the app (or container).

The usual actions of the boot dashboard work for those apps running in a container on docker as well. That way you can easily jump to the console output of a specific container, delete a container or image, stop it, or re-run it.

Restarting the container restarts the existing container. Restarting an application causes the boot dashboard to re-run the build and create a new image, followed by a new container that runs that new image. Side note: The image creation inside of the build process is smart enough to re-use existing layers and build only the necessary pieces of the container image.

Custom build commands (new in 4.9.0)

In case your project does not contain a Maven wrapper or you use a custom way to build a container image for your project, you can tell the IDE to execute that custom build instead of invoking the Maven wrapper. In order to do that, add a command line script to your project called sts-docker-build.sh that executes the build and produces the container image. For example, the script could look like:

#!/bin/bash
/usr/local/bin/mvn spring-boot:build-image

In case such a file exists in your project, the drag&drop operation to build and run a container image of your project on docker will use that script instead of the default built-in command.

default build and run commands (new in 4.9.0)

The Spring Tools execute specific commands by default in order to create an image for the project and then run that image in a container using docker. In order to tell you what the tooling does under the hood, the commands are displayed in the log output when executing them. That way you can see what is being executed and create a custom script (see previous section) if those default commands are not what you would like to use.

Debugging applications inside the container

Instead of just building the image and running the app in a container on docker, the boot dashboard also allows you to run the application inside of the container in debug mode and automatically connects the Eclipse debug UI to that process. That way, debugging the app when running inside of a container becomes pretty much the same easy experience than running the project locally in debug mode.

Spring Boot Devtools

The Spring Boot Devtools allow you to work with Spring Boot projects while they are running and speeds up turnaround cycles quite a bit when you change code. This "automatic restart" feature of the Spring Boot Devtools is pretty useful, since you don't need to restart the app every time you make a change and would like to try it in the running app.

In case your project has the Spring Boot Devtools module as a dependency, the docker integration for the Spring Boot Dashboard will automatically setup everything you need to get the "automatic restart" feature working for projects running inside of a docker container. There is nothing special that you need to do. If the boot dashboard recognizes the devtools on the classpath of the project, everything will be setup automatically.

This uses the remote application capabilities of the Spring Boot Devtools. Therefore you will see a local Java process (the remote devtools client application) being started whenever you start the app in the container. This process takes care of the communication with the remote process (in our case the process of your app inside of the container). You can setup this manually, if you prefer to have full control over this feature, too.

Clone this wiki locally