Monthly Archives: April 2016

Spring Boot, Docker integration with IntelliJ and Windows

In this post I’ll give a more detailed guide for the Spring Boot and Docker initiative.

The final goal that I had in mind, was to have a Docker container that runs the packaged java application in a clean environment.
Support for remote debugging is included in the configuration, as well with both running and debug listening ports mapped/forwarded between the windows and docker machine. So in the end you can access the application as if it was running on your “localhost” 😀

Pre-requisites:
– Docker ToolBox for Windows (with VirtuaBox) Dowload link
– A Spring Boot or any Java project with Maven support will suite the purpose Getting Started with Spring Boot
– IntelliJ Docker plugin installed

After installing the Docker Toolbox (next > next > next…) we can jump right into creating a simple HelloWorld Spring Boot Web Starter project.
To do this in the fastest way, create a New Project > Maven and inside the pom.xml add the following dependency:

1
2
3
4
5
6
7
8
9
10
11
12
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

Next, add a “hello” package and a java class that will act as our Rest Controller (HelloController.java) in which we map the default “/” context path to a method that returns a default “Hello” String.

1
2
3
4
5
6
7
8
9
@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "This is Spring Boot with Docker!!";
    }

}

Create another class, with a main method that will be the entry-point to our application. The @SpringBootApplication makes everything smooth :)

1
2
3
4
5
6
7
8
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

This is my trimmed version of the “Getting Started with Spring Boot” from the official site.
You can go ahead and run “mvn package” command to generate the application’s executable .jar file.

Now for the Docker part, let’s start with installing the IntelliJ “Docker integration” plugin. This offers easier communication with the docker and saves some time, cause you won’t have to spam “docker build” … “docker run” commands in a separate console.

In the Maven pom.xml file, you’ll need to add the following docker-maven-plugin along with a few very important configuration lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.5</version>
                <configuration>
                    <dockerHost>https://192.168.99.100:2376</dockerHost>
                    <imageName>spring</imageName>
                    <baseImage>java</baseImage>
                    <env>
                        <server.port>8888</server.port>
                        <java.security.egd>file:/dev/./urandom</java.security.egd>
                    </env>
                    <exposes>
                        <expose>8888</expose>
                        <expose>5005</expose>
                    </exposes>
                    <entryPoint>["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005", "-jar", "/${project.build.finalName}.jar"]
                    </entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

In the configuration we are able to specify the baseImage and imageName along with the expose and entrypoint parameters which are very common in a Dockerfile. In our case, we won’t be needing one at all, because the maven plugin will do all the work for us!

The environment variables are used to specify the server.port to run on 8888 instead of the default 8080 (yes, I’m still using Skype…)
The ports also need to be exposed from the docker container so both the server and remote debugging ports are added.
Entrypoint is basically the whole java command used to run the server, in our case the resulting “.jar” file after mvn package. It contains some extra arguments to enable remote debugging for the server.

In order to make use of this plugin we need to run “docker:build” from maven/plugins section.

Docker Build mvn
After it’s finished we will have a new “spring” image derived from the default Java one. Now here comes the Docker and IntelliJ integration part.

Docker plugin

We will go ahead and create a new Docker Deployment configuration as follows.
Docker Run configuration

We can then press Play and our container will be started along with the application server. In order to be able to access the server from Windows, we need to forward the 8888 and 5005 port respectively, from the docker machine to running Windows OS (this isn’t necessary if you’re a Linux guy, because the docker-host is running natively).

To do this we can use the following commands from an “elevated windows command prompt” (just run it as Administrator):

1
2
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=8888 connectaddress=192.168.99.100 connectport=8888
netsh interface portproxy add v4tov4 listenaddress=127.0.0.1 listenport=5005 connectaddress=192.168.99.100 connectport=5005

Everything is set and we can now access our application deployed inside the docker container from windows: http://localhost:8888

You can find the code here: SpringDocker Project

Hope you enjoyed this tutorial,
-M.