DEV Community

Cover image for Dockerizing a Spring Boot Microservice on WSL: My Learning Journey
Om Patil
Om Patil

Posted on • Edited on

Dockerizing a Spring Boot Microservice on WSL: My Learning Journey

Recently, I Dockerized my Spring Boot microservice and ran it successfully using WSL (Windows Subsystem for Linux). I also connected it to a PostgreSQL database running on my Windows host. It wasn't all smooth sailing—but I learned a ton, and I'm sharing it here to help fellow developers!


🛠️ My Environment

  • OS: Windows 10 with WSL (Ubuntu)
  • App: Spring Boot Microservice
  • Database: PostgreSQL on Windows
  • Tools: Docker, Maven

📦 Step-by-Step Journey

1️⃣ Create a Dockerfile

Here's the Dockerfile I used to containerize my app:


Dockerfile
FROM openjdk:23-jdk-slim
WORKDIR /app
COPY target/myservice.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
✅ Tip: I used openjdk:23-jdk-slim because my app runs on Java 23.

2️⃣ Build the JAR
I packaged my Spring Boot app using:
mvn clean package -DskipTests

I initially tried ./mvnw but switched to mvn since I already had Maven installed on WSL.

3️⃣ Build the Docker Image
docker build -t jwt-service .

This built my custom image containing the application JAR.

4️⃣ Database Connectivity Challenge
Initially, I used this in application.properties:

spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/myservice_db
But it failed inside WSL.

✅ Fix: I got my actual local IP using ipconfig on Windows and updated it:

spring.datasource.url=jdbc:postgresql://my_ip:5432/myservice_db
5️⃣ PostgreSQL Access Errors
I ran into these issues:

❌ Connection timed out

❌ no pg_hba.conf entry for host...

Fixes:

Edited pg_hba.conf to allow my IP.

Set listen_addresses = '*' in postgresql.conf.

Restarted PostgreSQL on Windows.

Tested it using a temporary PostgreSQL container:

docker run --rm -it postgres:16 psql -h my_ip -U postgres -d myservice_db
Worked perfectly!

6️⃣ Running the App in Docker
docker run -p 8080:8080 --name myservice-container myservice-service
Accessed the API at:
http://localhost:8080/myservice/service

7️⃣ Keep Container Running After Closing WSL
I used -d to run the container in the background:

docker run -d -p 8080:8080 --name jwt-container jwt-service

Or detached from a live container with:

Ctrl + P then Ctrl + Q

🚪 This keeps the container running while exiting the terminal.

🔍 What I Learned
✅ How to Dockerize a Spring Boot app
✅ How to connect from WSL Docker to host PostgreSQL
✅ How to configure pg_hba.conf and postgresql.conf
✅ Using psql inside a container to test connectivity
✅ Running containers in detached mode

🧠 What’s Next?
I plan to fully Dockerize the database as well, and eventually use Docker Compose to orchestrate the full microservice stack.

🙌 Final Thoughts
This wasn't just about Docker commands—it was about solving real-world issues like network configs, file access, and container persistence. I hope this helps someone else facing similar issues.

Feel free to drop your feedback or ask any questions in the comments!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)