In this quick tutorial we will learn how to dockerize a Spring Boot application using Docker.
Open the downloaded project in your IntelliJ IDEA and run the command mvn clean compile to check if the project is working.
We will check if our Spring Boot application is also running. Let’s run the next command in the internal terminal in IntelliJ (run it from your cmd or Git Bash in Windows):
mvn clean spring-boot:run
curl -v localhost:8080/api/hello
You should see the following message in your cmd to validate that the app is already running and returning the proper message in this route:
Now it’s time to create the configuration to dockerize our Spring Boot application using Docker.
Just to make it clear, Docker is a platform that combines your application, its dependencies, and even an OS into an image that can run on any platform.
Now we will create a Dockerfile to add all the configurations to dockerize our Spring Boot application.
Create a Dockerfile and add the next configurations (Docker will read as a pipeline to apply them):
Now I will explain a little bit about these commands:
That’s all for the configuration.
Now we need to generate our .jar file running the mvn install command in your IntelliJ terminal. This will generate a .jar file with all the classes from our application.
When the process is finished, go to the target folder from your project and see the spring-boot-docker.jar file there.
Great, we’re done with the preparations, and it’s time to build our image using Docker Engine.
Make sure you have Docker Engine installed. To check it, run the docker ps or docker info command on the terminal screen. If the command is not found, you may need to install Docker first. In this case please follow this link and find the installer for your OS.
Run your Docker Engine. Find the folder with the Dockerfile of your Spring Boot project in the terminal and execute the following command (make sure to end the command with a space and a dot):
docker build -t spring-boot-docker:spring-docker .
The “build” command will build an image according to the instructions that we passed to the Dockerfile and the -t flag is used to add a tag for our image.
In a few minutes you will see that the image was successfully created:
Run the docker image ls command to check if the image exists. You should be able to see your image in the repository.
Now we will run our image in a container, but first we will make sure we won’t get an error trying to point our container port with the localhost one.
Run the next command:
docker run -p 8080:8080 spring-boot-docker:spring-docker .
You may add the -d flag before -p to avoid seeing any logs and run the container in the background mode.
The flag -p creates a firewall rule which maps the previously exposed container port :8080 to the :8080 port on your machine.
If everything is done right, our container should be running.
Then run the curl -v localhost:8080/api/hello command in your terminal to test if we can access the endpoint of the application.
You must see the same response as in the beginning of the tutorial.