Series "Developing My Own Blog Website" is generated by ChatGPT.
In the first part of this series, we laid the foundation for our blog website using Spring Boot, SQLite, and Thymeleaf. Now that the core functionality is up and running, it’s time to Dockerize the application. Dockerizing the app makes it easier to deploy and ensures it runs consistently across various environments.
Why Docker?
Docker offers several key benefits for our Spring Boot project:
- Portability: It allows us to run the application consistently across development, staging, and production environments.
- Simplicity: Docker packages the application and its dependencies in a single container, reducing setup complexity.
- Consistency: With Docker, we avoid the common issue of "it works on my machine."
- Isolation: The app runs in a container that is independent of the host system, avoiding conflicts with other services.
Now let’s walk through the process of Dockerizing the Spring Boot blog application, with the added feature of mounting an external SQLite database file.
Step 1: Create the Dockerfile
The first step is creating a Dockerfile
in the root of the project. The Dockerfile
defines the environment and instructions to build the Docker container.
Here’s the content for the Dockerfile
:
# Use an official OpenJDK runtime as a parent image
FROM openjdk:17-jdk-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy the Spring Boot JAR file from the target directory to the container
COPY target/my-blog-app.jar /app/my-blog-app.jar
# Create a directory for the SQLite database inside the container
RUN mkdir -p /app/db
# Expose the port the app runs on
EXPOSE 8080
# Run the Spring Boot application
ENTRYPOINT ["java", "-jar", "my-blog-app.jar"]
Explanation of the Dockerfile
:
- FROM openjdk:17-jdk-alpine: Uses the lightweight OpenJDK 17 base image.
- WORKDIR /app: Sets /app as the working directory within the container.
- COPY target/my-blog-app.jar /app/my-blog-app.jar: Copies the generated JAR file into the container.
- RUN mkdir -p /app/db: Creates a directory inside the container to store the SQLite database file.
- EXPOSE 8080: Informs Docker that the application listens on port 8080.
- ENTRYPOINT: Runs the Spring Boot JAR inside the container.
Step 2: Update the application.properties
for SQLite
In order for Spring Boot to know where to look for the SQLite database inside the container, you need to configure the application.properties
file.
Update application.properties
as follows:
spring.datasource.url=jdbc:sqlite:/app/db/my-blog.db
spring.datasource.driver-class-name=org.sqlite.JDBC
This configuration tells Spring Boot to use SQLite and to expect the database file in the /app/db/ directory inside the Docker container.
Step 3: Build the Spring Boot Application
Before we can build the Docker image, we need to make sure that the application is packaged as a JAR file. To do this, use Maven’s clean package
command:
mvn clean package
This will generate a JAR file (e.g., my-blog-app.jar
) in the target
directory.
Step 4: Build the Docker Image
With the Dockerfile
in place and the JAR file generated, the next step is to build the Docker image. Run the following command in the root directory of your project:
docker build -t my-blog-app .
Explanation of the command:
docker build
: Builds a Docker image from the Dockerfile.-t my-blog-app
: Tags the image with the name my-blog-app..
: Specifies the current directory as the context for building the image.
Once the build completes, Docker will have created the image containing your Spring Boot application.
Step 5: Mount the External SQLite Database File
We want to use an external SQLite database file from the host machine to make sure the data is persistent and accessible even if the container is stopped or recreated. To do this, we’ll mount the SQLite database file from the host system to the Docker container using Docker volumes.
Let’s assume your SQLite file (my-blog.db
) is located in a directory on your host machine (e.g., /path/to/db/
).
Run the following command to start the Docker container while mounting the SQLite file:
docker run -p 8080:8080 -v /path/to/db/my-blog.db:/app/db/my-blog.db my-blog-app
Explanation of the command:
-p 8080:8080
: Maps port 8080 on your host machine to port 8080 in the container.-v /path/to/db/my-blog.db:/app/db/my-blog.db
: Mounts the local SQLite database file (/path/to/db/my-blog.db) to the /app/db/my-blog.db location inside the container.my-blog-app
: The name of the Docker image we built earlier.
This will ensure that the application uses the external SQLite file from your local machine while running inside the Docker container.
Summary of Commands
Here’s a summary of the key commands used during the Dockerization process:
- Build the Spring Boot JAR:
mvn clean package
- Build the Docker Image:
docker build -t my-blog-app .
- Run the Docker Container with Mounted SQLite Database:
docker run -p 8080:8080 -v /path/to/db/my-blog.db:/app/db/my-blog.db my-blog-app
Conclusion
By Dockerizing our Spring Boot blog application and mounting an external SQLite database file, we ensure both portability and data persistence. The mounted database file allows us to persist data between container restarts and easily manage the SQLite database outside of the Docker container.
In the next part of this series, we’ll explore how to deploy this Dockerized application to a cloud provider, making it available online for everyone to access. Stay tuned!