{{announcement.body}}
{{announcement.title}}

Dockerizing Spring Boot Application

DZone 's Guide to

Dockerizing Spring Boot Application

Take a look at how you can create a Docker container and image for your Spring Boot application in just a few simple steps.

· Cloud Zone ·
Free Resource

This blog will help you in Dockerizing your Spring Boot application. A sample project is commited on Github using this tutorial. Click here to check the project.

Prerequisites:

  • Docker must be installed on your machine.
  • You have basic knowledge of Spring profiling.

1. Add DockerFile & Entrypoint in Your Project:

Create src/main/docker  in your project and add DockerFile in your project. This file is not a project-specific file; all you need to change is MAINTAINER  and LOG_PATH . We are using openjdk8-slim as the base image. For security, we are creating a separate user for the container.

Change your BOOTAPP_JAVA_OPTS according to your need.

Image title

  • Add the entrypoint file wrapper.sh  in src/main/docker 

#!/usr/bin/env bash
exec java -Djava.security.egd=file:/dev/./urandom $BOOTAPP_JAVA_OPTS -jar  -Dspring.profiles.active=$ENV_NAME $BOOTAPP_PATH --server.port=$SERVER_PORT > /dev/stdout 2>&1


2. Update Your POM:

  • Add the Docker registry in the properties section of your POM. This is where your Docker images will be pushed.
<docker.registry>javatechy</docker.registry>


  • At first, add the docker-maven-plugin by Fabric8 to your project’s POM:Image title


3. Build The Image:

To build the image do mvn clean install. This will build your Docker image and store it in your local Docker repository.

To check your image run docker images  .  

Image title

4. Push Your Image:

To Push your image use mvn docker:push . This will push your image to your remote container registry.

5. Play with Your Container:

To start your built container:

docker run -it -p 9000:8000 --name docker_boot_app -d javatechy/dockboot


This will start your container on port 9000 

Browse your application on http://localhost:9000/docker_boot/health

Check your container using docker ps -a 

Image title



In this blog, we learned how we can dockerize your spring boot application using docker-maven-plugin.




Topics:
spring ,docker ,tutorial ,maven ,plugin ,spring boot

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}