How to run Spring Boot with Docker, easy in 15 minutes

The tutorial shows you how to run Spring Boot with Docker. You know Docker is a platform for packaging, deploying, and running applications in containers. Java microservices is a good target for Docker. Docker allows you to build an image that contains your application or microservice. The image can be used to run containers on any OS with Docker installed. Let’s dig deeper!

How to run Spring Boot with Docker

Docker installation

This post doesn’t focus on how to install Docker on a local machine. If your OS is Windows, please refer to the installation guide Docker for Windows. Otherwise, you can choose the correct installer for your OS and follow the instructions here.

Since you have done the installation. You can very your Docker version like this:

Running hello-world example on Docker, simply it runs an image named hello-world

As you see the above output, Docker looked for a local image named hello-world since that image is not present it go to Docker Hub and download the latest image. Once the downloading completes it runs hello-world in a container.

So far, your docker works well.

Spring Boot application

This post just focuses on Docker so we will use the existed project from the post Spring Boot FreeMarker Hello World Example. This is a standalone application, like this:

Spring Boot FreeMarker Hello World Example

Build and run a Docker application

Here are steps on how to run Spring Boot with Docker.

Defining the Docker Image

The Docker image is defined in the Dockerfile below

Note: Here we are using source code from Spring Boot application with Freemarker. It needs to package to war file so you see the war file in the command ENTRYPOINT [“java”, “-jar”, “app.war”]. Whereas you just deploy the poor spring boot application, it just needs to package to a jar file.

Building the Image

You have the Docker image defined in Dockerfile that is located in the project root directory. Going to the project root directory and run the command on the console.

Hit that command and see the below output.

How to run Spring Boot with Docker

You will see the image listed by running the below command.

Running a Container

Now you can run your image in a Docker container with the command

If everything works fine, you hit the command on console, it will produce the

How to run Spring Boot with Docker

When you see that output it means your container start normally. Now you can test your spring boot application to see how it works on Docker container. You just hit the url http://localhost:8080 on the browser. The response looks like this.

Now, you have run your application on Docker container successfully.

Happy coding!

That’s all about the tutorial on how to run Spring Boot with Docker.


Docker for Windows
Download complete source code, click link below (114 downloads)

Please share it if you found this useful
Hide Buttons