This project demonstrates how to containerize a Spring Boot application using Docker. The Dockerfile provided allows you to build and run a Spring Boot application in a Docker container, ensuring consistent environments for development, testing, and production.
.
├── src/ # Source code of the Spring Boot application
├── pom.xml # Maven project configuration file
└── Dockerfile # Dockerfile for building the Docker image
- Docker installed on your machine.
This project uses a multi-stage Dockerfile to build and package the Spring Boot application:
-
Build Stage:
- Uses the
maven:3.8.3-openjdk-17image to build the project. - Copies the source code and
pom.xmlinto the container. - Runs
mvn clean packageto build the JAR file.
- Uses the
-
Package Stage:
- Uses the
openjdk:17image to create a lightweight container. - Copies the JAR file from the build stage into the container.
- Exposes port
8080. - Sets the user to
10014for running the application. - Defines the entry point to run the Spring Boot application using the JAR file.
- Uses the
To build the Docker image, navigate to the directory containing the Dockerfile and run the following command:
docker build -t springboot-docker .After building the Docker image, you can run the container using:
docker run -d -p 8080:8080 springboot-dockerThis command will run the Spring Boot application in a Docker container and map port 8080 of the container to port 8080 on your host machine.
The application includes a simple REST controller that responds with a "Hello World" message. To test the application:
- Open your web browser or use a tool like curl or Postman.
- Navigate to the following URL:
http://localhost:8080/hello/message
- You should see the following response:
Hello World
This confirms that the application is running successfully in the Docker container.