How to run a Spring Boot Application in Docker

In this tutorial, we will learn about How to run the Spring Boot application using Docker. I hope all you have installed Docker to your machines. If you have not installed Docker into Your machine please go through my previous tutorials related to Docker.

We are going to do these things,

  • Create an executable JAR — Spring Boot automatically does this thing using Spring Boot Maven plugin.
  • Create a Docker file — Docker file is a kind of a blueprint that contains all the setup and instructions to build your Docker Image.
  • Build Docker Image — We are building a Docker image by running the Docker File
  • Run docker container — Finally, run the Project in Docker

So those are the 4 basic steps we are going to follow.

Create an executable JAR

Let me create a very simple Spring Boot Application. If you are not familiar with creating a Spring Boot application please go through my previous tutorials related to creating a Spring Boot Application.

So I have created a simple Student Management System. I named it as “Spring UnitTestApplication”. Sure you will think about why I have used this Unit Test word for the Project name. Because I am going to use this application to the next upcoming tutorial related to Unit Testing with JUnit and Mockito.

Okay, now I am ready with my Spring Boot Application. First thing you have to make sure in the pom.xml file you should be able to see something like “spring-boot-maven-plugin”.

This spring-boot-maven-plugin is responsible for creating a single executable JAR file. To building this project you should be able to see that JAR in the target directory. I am going to build my project now.

So in the terminal please type this command and build the JAR file.

$ mvn clean install

Now you should be able to see your JAR in the target directory.

unittest-0.0.1-SNAPSHOT.jar file

Now the good thing is you can run this JAR file and run the application just like a console application. So let me show you what I mean.

Now you can run your Application using console/terminal. The reason why I am showing this is that in order to make this application dockerize all we have to do is move this JAR file to the respective Docker Image and the Docker container will take care of the rest.

Create a Docker File

Go to the root directory and create a file called DockerFile. I have added some codes to this file and let me explain those lines.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/unittest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app. jar"]
  • “FROM openjdk:8-jdk-alpine” — It says that we are building our Docker Image from the base Image openjdk. So we are taking openjdk as a parent Image. we are building out an application on top of that.
  • “VOLUME /tmp” — This line is needed for application because Tomcat try to create a work folder inside this volume.
  • “ADD target/unittest-0.0.1-SNAPSHOT.jar app.jar” — Here we are adding our JAR which is inside out to target location to the Docker Image in the name of app.jar .
  • “ENTRYPOINT [“java”,”-jar”,”/app. jar”]” — This is for an entry point our application to run. This is what we are already done earlier(Run application using console/terminal). Here we are giving some instructions on how to run our application.

Build our Docker Image

To build your application Docker Image go to the project folder and open the console/terminal. Before building our docker image please make sure your Docker setup is up and running.

Now we are going to build our Docker Image using DockerFile. Now use this command to build your Docker Image.

$ sudo docker build -t <projectname in lowercase> <Docker File location>

When you are running this command the first time it will get some time to download parent Images. Now you can check whether your Docker image is created successfully or not using this command.

Here you can see it is showing all the available Images in your machine. Check your newly created Image. My image “springunittestapplication” is showing on the list.

Now you have created Docker Image successfully. The last and final step is Run Docker Container.

Run Docker Container

Now you need to up your application and run. Before running that you need to do port mapping. Your application running in a virtual environment. If you try to access your host machine port it will not give access. For that, you have to do a port mapping from your host machine to docker-machine. You can use any port.

Example :

-p8080:8080

sudo docker run -p8080:8080 springunittestapplication

Now when we run this command Docker will create a new container and it will start our application (Start the container).

So that is pretty much about how to dockerize our application. Now if you want to stop the container first you need to get the Docker container ID.

 $ docker ps

This will give you all the running containers. Now copy the container ID and use this command to stop the Docker container.

$ docker container stop <Container ID>

Now If you want to start again your container do not use the “docker run” command. It will create a new container. You can start the existing container. So in order to start the existing container use this command.

$ docker container start <Container ID>

So this is about basic steps about how to Dockerize your Spring Application. See you next in another tutorial.

Thank You!

Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Software International