DEV Community

Cover image for Containerize A Spring Boot App with Docker

Containerize A Spring Boot App with Docker

scottshipp profile image scottshipp Originally published at ・2 min read

Also see my other Spring Boot 2 tutorials!

Who this tutorial is for

This tutorial is for anyone who is wondering how to take a Spring Boot 2 application and containerize it with Docker.


Before you begin, either make sure you have an existing Spring Boot 2 application, or if you don’t, I recommend going through the Spring Guide “Building a RESTful web service” with Spring Boot.

Once you have that, return here.

Video Version

As an added bonus, check out the end of this tutorial, where I’ve placed a video version of all of this.

Step 1. Add a Dockerfile

First, add a text file to the root directory of your application named “Dockerfile.”

Copy and paste the following into it:

FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

NOTE: if you are using Java 8, or another version of Java, be sure to select the correct base for your image in the “FROM” line (line 1). You can find other openjdk container bases at Docker Hub.

You may also be happy to know that there’s a Spring Guide titled “Spring Boot with Docker” where you can find more information about Dockerfiles and Spring Boot.

Step 2. Build an image

Next, you will build an image. Open a terminal, navigate to the root directory of your application, and type:

docker build -t {tag}

You can read more about tags in the Docker documentation.

If you plan to use Docker Hub, then it’s common to use your Docker Hub username. For example, I used this command on my example:

docker build -t scottashipp/helloworld

Step 3. Create and run a container

Now that you have built an image, you can create and run a container from that image.

In the same directory, type:

docker run -p 8080:8080 {tag}

If you forgot how you tagged your image, try listing images:

docker image ls

Assuming you find your tag, and run it correctly (for example I used “docker run -p 8080:8080 scottashipp/helloworld”), you should see Spring Boot start up, and the familiar “Started application . . .” message.

In this tutorial

In this tutorial, you have:

  • Built and tagged a Docker image from an existing Spring Boot application
  • Created and ran a Docker container from the created image

Next Steps

Next, check out the video version! Make sure to like and subscribe!

Discussion (7)

Editor guide
barddoo profile image
Charles Fonseca
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar

what does those lines do?

stealthmusic profile image
Jan Wedel

They copy the built spring boot executable jar file from the target folder in your project into the docker image and rename it to app.jar.

barddoo profile image
Charles Fonseca

Does it merge all jars into one, which is app.jar?

Thread Thread
scottshipp profile image
scottshipp Author

After you build, look in the target/ folder, you will see a jar named the name of your service + '-' + version. For example, if your service is named helloworld, and on version 0.1.0-SNAPSHOT it will be named helloworld-0.1.0-SNAPSHOT.jar.

This is the jar that will be copied into the Docker container as app.jar

Thread Thread
barddoo profile image
Charles Fonseca

Thank you for explain!

Thread Thread
stealthmusic profile image
Jan Wedel

And yes, all dependencies will be inside the one app.jar. This is usually done by the spring boot maven plug-in.

rphilogene profile image
Romaric Philogène

Hi Scottshipp, thanks for sharing. I would really love to have your feedback on my how to deploy a containerized Spring Boot application on AWS with Qovery.