Adding docker support in Spring Boot application

Adding docker containerization support in a working Spring Boot application is surprisingly easy.

Step 1: Add the docker maven plugin to your pom file.

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.4</version>
            <configuration>
                <repository>${project.artifactId}</repository>
            </configuration>
        </plugin>
    </plugins>
</build>

Step 2: Build the application as well as the docker image

mvn clean package install dockerfile:build

Step 3: I have a local docker registry setup in my Lan so I prefer to push this image to my network registry manually (this can be automated as well however).

docker tag my-app:latest my_network_registry_host:5000/my-app

Step 4: Now we can bootup this docker image anywhere in the network.

docker run \
--detach \
--link <any database link> \
-e <Environment variable 1> \
-e <Environment variable 2> \
-p 8080:8080 \
--name my-app \
my_network_registry_host:5000/simply-hr
 Step 5: Checkout the application URL @ http://localhost:8080. Tail the log if you want to
docker logs --follow simply-hr

Leave a Reply

Your email address will not be published. Required fields are marked *