DEV Community


Posted on • Updated on

Dockerize your Spring Boot Server

There are many reasons why you want to run your spring boot server in a docker container. For example a capsuled environment, the comfort to delete and rebuild your application or a easy way to autostart your service on every reboot. I hope I have wake up some motivation in you to dockerize your spring boot server so we can start.
But before let me tell there exists many different ways to structure the dockerfile and my way must not be the best way in your specific case.

Docker vs VMs

When you search for a way to dockerize your application you have been hear something about VMs by sure. If not a VM, short for virtual machine, is a virtual operating system on your real operating system. You can give them some resources like process units from your processor, some ram and storage. Further you can install a operating system of your choice. This call like a lot of configuration by your own. Docker is easier in that way. If you have written a dockerfile (a blueprint of your docker container) you can easily build your whole docker container on any machine with a few CLI commands. That's all. Now let us learn something about the basic component of a dockerized app.

Docker components

First we have the dockerfile. A dockerfile is the blueprint to tell docker how your app should be build and which third-party services your application will be need.
Secondly there is the docker image. The docker image is the builded package of your application. Similar to an .jar, .exe or .deb file but you can capsule other images in your image like a java runtime environment. The best way to find docker images is to take a look on docker hub.
The third and last component is the docker container. The docker container is like a vm but needs lower resources to run. In the docker container runs your application. You can start and stop the container. On every start of it the applications in it are restarting too.
So after we know the core components of docker we can start to write the dockerfile for our spring boot server.

Writing a dockerfile

Create a new file called 'Dockerfile'. Then open the file with a text editor.
We started with the first line FROM eclipse-temurin:latest. The statement FROM means that a docker image will be used. In our case we are using the image of a java runtime environment. latest in this case means that we will use the latest release of the image at every time we are executing the dockerfile to build an image. The official wording for the release version is tag. If you want to use a specific tag for example '17.0.7_7-jdk' write FROM eclipse-temurin:17.0.7_7-jdk.
The next line is the EXPOSE statement to define a port your application want to use. Also write EXPOSE 8080 if your spring boot application is using the default port 8080. Otherwise replace 8080 with the correct port of your application.
The next step is a little more complex than the steps before. For this tutorial you will need your spring boot server as compiled .jar file. If you have a gradle project you can do it with the CLI command ./gradlew build.
After you have your jar file we can continue to add your jar to the image. For this we need to expand our dockerfile by two new lines. With the line ARG JAR_FILE=target/*.jar we define an argument that we call 'JAR_FILE'. If now argument is provided in the build command docker will use the provided default value target/*.jar. But this line is only one half. Further we need the next line ADD ${JAR_FILE} app.jar. This add your provided jar file to the builded docker image with the new name 'app.jar'.
Coming to the last line we only need the ENTRYPOINT statement to start the spring boot application inside of the container. Also we add the line ENTRYPOINT ["java", "-jar","app.jar"]. Translated this means simply use java to run the jar with name app.jar.
And this is it. You have written your first dockerfile. Your dockerfile should look like this now.

FROM eclipse-temurin:latest
ARG JAR_FILE=target/*.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar","app.jar"]
Enter fullscreen mode Exit fullscreen mode

Build the docker image

After you have your dockerfile ready we can start to build the docker image. For this open a terminal and check if docker is running. For example use the command docker ps -a to list all docker containers. If docker is running you can start to build your image with the following command:

docker build --build-arg JAR_FILE=build/libs/example.jar -t springboot:latest .
Enter fullscreen mode Exit fullscreen mode

Replace the jar path after JAR_FILE= with the path to your own jar file, springboot with the name of your new docker image and latest with tag of your choice but you can also leave it as latest.
Important! Don't forget the dot at the end of the command.
Now docker will build your image. At this process some third party docker images like the jre will be downloaded by docker.

Run the docker container

Finally you can run your docker image in a docker container. Use the following command to do that:

docker run --name springbootcontainer -d -p 8080:8080 springboot:latest
Enter fullscreen mode Exit fullscreen mode

Like in the steps before replace springbootcontainer by the name of your new container and springboot:latest by the name of your image and your tag. With the -p parameter you can define which port from inside of the container should forwarded to which port of your local machine. In common situations you forwarded the application port to the identical port of your local machine.
Hint: If you want automatically restarts of your docker container use the additional parameter --restart always in the docker run command.


Congratulation! You have build your first docker container of an java spring boot application. Now you can start to experiment with the dockerfile elements and try other ways to deploy your docker container to match your case perfectly.
If you need more input try the documentation of docker.

Top comments (0)