How do you create a Spring Boot application with Docker?
Table of Contents
- Introduction
- Steps to Create a Spring Boot Application with Docker
- Conclusion
Introduction
Docker provides an easy and efficient way to package and deploy applications in containers. It simplifies the development and deployment process by allowing you to encapsulate your application and its dependencies in a single, portable container. This is especially useful in microservices architectures where different services can be containerized and deployed independently.
In this guide, we will demonstrate how to containerize a Spring Boot application using Docker, step-by-step. You will learn how to create a Spring Boot application, build a Docker image, and run it as a Docker container.
Steps to Create a Spring Boot Application with Docker
1. Create a Spring Boot Application
First, let's create a simple Spring Boot application. You can either generate a Spring Boot project from Spring Initializr or manually create it using your favorite IDE.
For this example, we will create a simple RESTful service that returns a greeting message.
Application Code (GreetingController.java
):
application.properties
(Optional Configuration):
Once your Spring Boot application is ready, you can build it into a JAR file using the following command:
This will create a .jar
file in the target/
directory (e.g., docker-0.0.1-SNAPSHOT.jar
).
2. Create a Dockerfile
The next step is to create a Dockerfile
in the root directory of your Spring Boot project. A Dockerfile contains instructions for building a Docker image for your application.
Here is an example of a simple Dockerfile
for a Spring Boot application:
Dockerfile
Explanation of the Dockerfile:
FROM openjdk:17-jdk-slim
: This line specifies the base image, which in this case is a slim version of the OpenJDK 17 image.WORKDIR /app
: Sets the working directory for the application inside the container.COPY target/docker-0.0.1-SNAPSHOT.jar app.jar
: Copies the JAR file created in the previous step into the container.EXPOSE 8080
: Exposes port 8080, which is the port on which the Spring Boot application runs.ENTRYPOINT ["java", "-jar", "/app/app.jar"]
: Specifies the command to run when the container starts.
3. Build the Docker Image
Now that the Dockerfile
is created, you can build the Docker image using the docker build
command.
Open a terminal in your project directory (where the Dockerfile
is located) and run the following command:
-t spring-boot-docker
: The-t
flag tags the image with a name, in this case,spring-boot-docker
..
: The dot specifies that the build context is the current directory.
Docker will read the Dockerfile
, install all dependencies, and create the Docker image. Once the process is complete, you will see a message indicating that the image was successfully built.
4. Run the Docker Container
Now that the image is built, you can run the Spring Boot application inside a Docker container.
To run the Docker container, use the docker run
command:
-p 8080:8080
: Maps port 8080 on the host machine to port 8080 in the container, allowing you to access the application.spring-boot-docker
: The name of the Docker image you built earlier.
The container will start, and your Spring Boot application will be accessible at http://localhost:8080/greet
.
5. Verify the Application
Open your browser or use a tool like curl to access the /greet
endpoint:
You should see the following response:
This confirms that your Spring Boot application is successfully running inside a Docker container.
6. Optional: Docker Compose (for Multi-Container Applications)
If you need to run multiple services in different containers (for example, a database and a Spring Boot service), you can use Docker Compose.
Example docker-compose.yml
:
In this example:
- The Spring Boot app (
spring-boot-app
) is linked to a MySQL container (db
). - The
docker-compose.yml
file defines how the two containers should interact. - You can start the multi-container setup with:
Conclusion
By following these steps, you have successfully containerized a Spring Boot application with Docker. Docker allows you to package your application and its dependencies into a single, portable container that can be run anywhere. This approach simplifies deployment and scaling, especially in microservices architectures.
Whether you're working on a small application or a large microservices ecosystem, Docker is an essential tool for building, testing, and deploying your Spring Boot applications in a consistent and isolated environment.