Deploying a Spring Boot Microservice to Docker: A Quick Guide
Learn how to quickly and easily get your microservice hosted on a Docker container.
Join the DZone community and get the full member experience.Join For Free
In this article, we will deploy our Spring Boot microservice to Docker. In our previous article, we had created a simple "Hello World" Spring Boot microservice. Now, we will deploy that application into Docker.
Docker with Spring Boot is currently a very popular technology stack which enables organizations to seamlessly develop and make production ready artifacts.
Before deploying the application to Docker, make sure you have installed Docker. In this example, we have used Docker Community Edition (Docker CE) on Windows OS. How to install docker on Windows/MacOS/Linux. We can also use Alpine Linux image, as it provides a minimal Linux environment to deploy and run the application. There are a few Docker commands to manage your application. The below sample command and screen shot shows how to check the version of your installed Docker engine.
What We Need to Deploy a Spring Boot application in Docker
First, we need to create an Image file for our application. Docker images are an important component for working with the Docker engine. Docker provides a Docker Hub, which is a library and community for container images. We can use Docker Hub to get the most common images. In the below project structure, we have created a "Dockerfile.txt". If you put this Dockerfile into the class path, then the Docker engine will automatically identify and load this file. Dockerfiles' names are very sensitive, so you must follow the naming convention as mentioned in the below screen shot. Docker reads commands/instructions from "Dockerfile.txt" and builds the image.
The below Dockerfile contains the commands to create the image. Actually there are many commands used for different purposes. Here we have used a few commands as per our needs.
- FROM - Must be the first non-comment instruction in the Dockerfile. This command creates a layer from the Docker image. In our case, we have used
java:8which means this application will run on Java 8.
- EXPOSE - Exposing the port for the endpoint. In this example, we have configured 8080.
- ADD - This command helps to take a source and destination. Normally, the source is your local copy. The
COPYcommand also does same thing, but there is a small difference between the
- ENTRYPOINT - It's similar to CMD, where our command/jar file will be executed.
- FROM - Must be the first non-comment instruction in the Dockerfile. This command creates layer from the Docker image. In our case we have used
java:8, which means this application will run on Java 8.
Now, run the command to build the image and deploy it to Docker. Before running the Docker command, we need to create the .jar file. This is because we are creating a jar file and then creating the jar file as a Docker image. So, here we have used the
mvn clean install command to create the jar file. The below Maven command is used to create the jar file.
Now we can see the below jar has been created.
Create a Docker image file. The below command is used to create the image file.
docker build -t sample-hello-microservice-springboot .
As per the above screenshot, it seems we have created the Docker image successfully. You can check the created image by using the
"docker images" command.
Now, our image file is ready. We can push this image to Docker by using the below command.
docker run -p 8080:8080 -t sample-hello-microservice-springboot
Now our microservice has been deployed to Docker and its exposed on port 8080 as per our configuration in the Dockerfile. We can check the running container and its status.
Now we can also access from browser as below.
Below are a few more useful commands:
docker ps --help
docker run --help
docker system prune
docker ps -a
There are many commands with help options to get the help about each command. Read more.
Published at DZone with permission of Manoj Kumar Bardhan. See the original article here.
Opinions expressed by DZone contributors are their own.