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:
1 2 |
docker --version Docker version 19.03.5, build 633a0ea |
Running hello-world example on Docker, simply it runs an image named hello-world
1 2 3 4 5 6 7 8 9 10 |
docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. ... |
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:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# download maven image from Docker Hub FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD # author who maintains this file MAINTAINER javabycode.com # copy and packing source code in docker image COPY pom.xml /build/ COPY src /build/src/ WORKDIR /build/ RUN mvn package # download openjdk image FROM openjdk:8-jre-alpine # run war file in docker container WORKDIR /app COPY --from=MAVEN_BUILD /build/target/*.war /app/app.war ENTRYPOINT ["java", "-jar", "app.war"] |
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.
1 2 |
# -t gs-spring-boot-docker:latest that allows you tag a image with name docker build -t gs-spring-boot-docker:latest . |
Hit that command and see the below output.
You will see the image listed by running the below command.
1 2 3 4 5 6 |
D:\Projects\SpringBoot-Freemarker-Example>docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE spring-boot-freemarker-example latest 42b5322b114b About an hour ago 99.9MB openjdk 8-jdk-alpine a3562aa0b991 8 months ago 105MB hello-world latest fce289e99eb9 12 months ago 1.84kB maven 3.5.2-jdk-8-alpine 293423a981a7 2 years ago 116MB |
Running a Container
Now you can run your image in a Docker container with the command
1 2 3 |
# -p 8080:8080 option maps the containers internal port 8080 to port 8080 on the host machine. # spring-boot-freemarker-example:latest is tag name of image docker container run -p 8080:8080 spring-boot-freemarker-example:latest |
If everything works fine, you hit the command on console, it will produce the
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.
References
Docker for Windows
Download complete source code, click link below
SpringBoot-Freemarker-Example.zip (149 downloads)