Dockerfile and Steps to build Docker image for your Spring Boot project
To build a Docker image for your Spring Boot project, follow these steps:
Prerequisites
1. Docker installed on your machine.
2. A built Spring Boot JAR file in your `target` directory (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).
3. A Dockerfile in the root directory of your project (see the Dockerfile example below).
Step-by-Step Instructions
1. Navigate to the Root Directory of Your Project
Open a terminal and go to the root directory where your `Dockerfile` is located:
```bash
cd /path/to/your/project
```
2. Build the Spring Boot JAR
Make sure that the Spring Boot JAR file is available in the `target` directory. If not, build it using Maven:
```bash
mvn clean package
```
After running this command, a JAR file will be created in the `target` folder (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).
3. Build the Docker Image
Use the `docker build` command to build the Docker image:
```bash
docker build -t springboot-app .
```
- `-t springboot-app`: The `-t` flag is used to name the image. Here, `springboot-app` is the name of your Docker image.
- `.`: The period (`.`) at the end specifies the current directory as the build context, where the Dockerfile is located.
4. Verify the Docker Image
After the build is complete, verify that the image was created using the `docker images` command:
```bash
docker images
```
You should see an entry similar to the following:
```
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot-app latest 123abc456def 5 minutes ago 500MB
```
5. Run the Docker Container
Once the Docker image is built, you can run a container using the `docker run` command:
```bash
docker run -p 8080:8080 springboot-app
```
- `-p 8080:8080`: Maps port 8080 on your local machine to port 8080 in the Docker container.
- `springboot-app`: The name of the Docker image you built.
6. Access Your Spring Boot Application
Open a web browser and navigate to:
```
http://localhost:8080
```
You should see your Spring Boot application running!
Additional Tips
- Tagging the Image with Versions: You can tag the image with a specific version using `:version`:
```bash
docker build -t springboot-app:v1.0 .
```
- Running with Environment Variables: You can pass environment variables to the container using the `-e` flag:
```bash
docker run -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=prod" springboot-app
```
- Running the Container in Detached Mode: Use the `-d` flag to run the container in detached mode:
```bash
docker run -d -p 8080:8080 springboot-app
```
Here's a `Dockerfile` using `openjdk:17` as the base image and including environment variables configuration.
Dockerfile Contents
```dockerfile
# Use the official OpenJDK 17 image
FROM openjdk:17-jdk-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the Spring Boot JAR file into the container
COPY target/*.jar app.jar
# Expose the port that the Spring Boot application runs on (optional, defaults to 8080)
EXPOSE 8080
# Set environment variables (optional: add your specific environment variables here)
ENV SPRING_PROFILES_ACTIVE=prod \
JAVA_OPTS="-Xms256m -Xmx512m" \
APP_NAME="springboot-app"
# Run the Spring Boot application using the environment variables
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar app.jar"]
```
Key Components Explained
1. `FROM openjdk:17-jdk-slim`:
- Uses the official OpenJDK 17 image (`slim` variant) for a lightweight build.
2. `WORKDIR /app`:
- Sets the working directory inside the container to `/app`.
3. `COPY target/*.jar app.jar`:
- Copies the built Spring Boot JAR file (`*.jar`) from the `target` directory into the `/app` directory inside the container, renaming it to `app.jar`.
4. `EXPOSE 8080`:
- Opens port `8080` on the container to allow external traffic to reach the application. This is optional but helps document the expected port.
5. `ENV ...`:
- Adds environment variables to the Docker image.
- `SPRING_PROFILES_ACTIVE`: Sets the Spring Boot profile (e.g., `dev`, `test`, `prod`).
- `JAVA_OPTS`: Allows you to pass JVM options, such as memory settings or GC options.
- `APP_NAME`: A custom environment variable to hold the name of the application.
6. `ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar app.jar"]`:
- Runs the JAR file using `java -jar` and includes the specified JVM options (`JAVA_OPTS`).
- `sh -c` allows the `JAVA_OPTS` variable to be evaluated at runtime.
To build a Docker image for your Spring Boot project, follow these steps:
Prerequisites
1. Docker installed on your machine.
2. A built Spring Boot JAR file in your `target` directory (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).
3. A Dockerfile in the root directory of your project (see the previous Dockerfile example).
Step-by-Step Instructions
1. Navigate to the Root Directory of Your Project
Open a terminal and go to the root directory where your `Dockerfile` is located:
```bash
cd /path/to/your/project
```
2. Build the Spring Boot JAR
Make sure that the Spring Boot JAR file is available in the `target` directory. If not, build it using Maven:
```bash
mvn clean package
```
After running this command, a JAR file will be created in the `target` folder (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).
3. Build the Docker Image
Use the `docker build` command to build the Docker image:
```bash
docker build -t springboot-app .
```
- `-t springboot-app`: The `-t` flag is used to name the image. Here, `springboot-app` is the name of your Docker image.
- `.`: The period (`.`) at the end specifies the current directory as the build context, where the Dockerfile is located.
4. Verify the Docker Image
After the build is complete, verify that the image was created using the `docker images` command:
```bash
docker images
```
You should see an entry similar to the following:
```
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot-app latest 123abc456def 5 minutes ago 500MB
```
5. Run the Docker Container
Once the Docker image is built, you can run a container using the `docker run` command:
```bash
docker run -p 8080:8080 springboot-app
```
- `-p 8080:8080`: Maps port 8080 on your local machine to port 8080 in the Docker container.
- `springboot-app`: The name of the Docker image you built.
6. Access Your Spring Boot Application
Open a web browser and navigate to:
```
http://localhost:8080
```
You should see your Spring Boot application running!
Additional Tips
- Tagging the Image with Versions: You can tag the image with a specific version using `:version`:
```bash
docker build -t springboot-app:v1.0 .
```
- Running with Environment Variables: You can pass environment variables to the container using the `-e` flag:
```bash
docker run -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=prod" springboot-app
```
- Running the Container in Detached Mode: Use the `-d` flag to run the container in detached mode:
```bash
docker run -d -p 8080:8080 springboot-app
```