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.
Join the DZone community and get the full member experience.Join For Free
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.
- Docker must be installed on your machine.
- You have basic knowledge of Spring profiling.
1. Add DockerFile & Entrypoint in Your Project:
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
LOG_PATH . We are using openjdk8-slim as the base image. For security, we are creating a separate user for the container.
BOOTAPP_JAVA_OPTS according to your need.
Add the entrypoint file
#!/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.
- At first, add the docker-maven-plugin by Fabric8 to your project’s POM:
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 .
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
Browse your application on http://localhost:9000/docker_boot/health
Check your container using
docker ps -a
In this blog, we learned how we can dockerize your spring boot application using docker-maven-plugin.
Opinions expressed by DZone contributors are their own.