Spring Boot Docker Deployment – DZone Java

Spring Boot Docker Deployment opens the door for deploying our Spring Boot Microservices on Docker containers.

Let’s start the process as below.

What Is Docker?

Docker is a platform that enables developers to develop, ship, and execute applications in the form of containerized applications.

It does this by creating a lightweight executable package of your application. This package includes the application code as well as all the dependencies required by the application to run. Dependencies could be environment variables, libraries, tools, and so on.

Such an executable package is also commonly known as an image. And a running image is a container.

Docker is in itself a vast topic. In case you want to know more, I have a detailed post about the Basics of Docker.

Defining a Docker Image With Dockerfile

To create a Docker Image for our Spring Boot Docker Deployment, we need to create a Dockerfile in the root directory of our project.

Below are the contents of the Dockerfile for a typical Spring Boot application.

# Start with base image
FROM openjdk:8-jdk-alpine

# Add Maintainer Info
LABEL maintainer="Progressive Coder"

# Add a temporary volume

# Expose Port 8080

# Application Jar File
ARG JAR_FILE=target/spring-boot-starter-0.0.1-SNAPSHOT.jar

# Add Application Jar File to the Container
ADD ${JAR_FILE} spring-boot-starter.jar

# Run the JAR file
ENTRYPOINT ["java", "-jar", "/spring-boot-starter.jar"]

Let us walk through each step in the Dockerfile.

FROM: This statement is used to denote the base image that our new Docker image will be using. In the above example, we have used the OpenJDK 8 image. This is a very lightweight option to run Java applications.

LABEL: This instruction is used to add some type of meta-data to the image. In this case, we added the meta-data called MAINTAINER.

VOLUME: The Volume instructions create a mount point in the container. You can also map a mount point to a directory on the Host OS. A typical use-case of volumes is to store log files generated by our application.

EXPOSE: This is an important instruction. It allows us to expose a particular port to the outside world. In our example, we expose Port 8080.

ARG: This instruction is used to define a variable with a default value. In our case, we are setting it to the location of the JAR file.

ADD: In this instruction, we are basically copying files and directories to our docker image.

ENTRYPOINT: Last but not least, the ENTRYPOINT is where you configure how the application should be executed. In our case, we are specifying how to run the JAR file.

Creating the Docker Image

Now is the time to create the Docker image. To do so, we need to have the JAR file ready in our Spring Boot project area.

To create a JAR file, execute the below command.

mvn clean package

Now to build the Dockerfile, the below command needs to be run:

docker build -t spring-boot-starter .

Once this command is executed, you would be able to see the image being built. You can see the list of images if you execute the below command:

docker image ls

Run Docker Image

Now that the image has been successfully built, you can run the Docker image by using the Docker Run command.

docker run spring-boot-starter

Basically, we are asking Docker to run the image tagged as spring-boot-starter. In other words, this is the Spring Boot Docker Deployment we have been aiming for in this post.

This will create a container and the application will start running. To see a list of currently running containers, you can use the below command:

docker ps

The application can be accessed via http://localhost:8080

You can also change the port of the container by issuing the below command to run the container.

docker run -p 5000:8080 spring-boot-starter

Basically, here 5000 is the port on the Host OS. We are mapping port 8080 on the container to port 5000 on the Host OS.

If you wish to stop the running container, you can press CTRL + C.


With this, we have successfully created a Spring Boot Docker Deployment of our microservice. If you wish to know more about Spring Boot, please refer to this post.

We first created a Docker image and then ran it on the Host OS. The Docker Image can also be publicly shared through Docker Hub. Typically, enterprises also have internal Docker registries where images are stored for future use. We will explore those concepts in future posts.

The code till this point (with the Dockerfile) is available on Github.

Source link

Leave a Reply

Shopping cart


No products in the cart.

Continue Shopping