Lab 16: Containerize Your Microservices Using Docker and Manage Container Images (Spring Boot 3.4.1)
Learn how to containerize two Spring Boot 3.4.1 microservices (UserService
and OrderService
) with Docker. You will build Docker images, run containers locally, manage containers (start/stop/remove), and optionally push images to a public registry (Docker Hub).
-
Download Docker Desktop.
- Visit Docker Desktop and download it for your OS (Windows, macOS, or Linux).
-
Install Docker Desktop.
- Run the installer and follow on-screen instructions.
-
Start Docker Desktop.
- Once installed, open Docker Desktop.
- Confirm it’s running (Docker icon in your system tray/taskbar).
-
Verify Docker installation.
- In a terminal or command prompt:
docker --version
- Confirm Docker is installed and you see a version number.
- In a terminal or command prompt:
-
Ensure
UserService
andOrderService
are ready.- Each microservice is a Spring Boot 3.4.1 app with a simple controller.
-
Add a Dockerfile to
UserService
.- In the root directory of
UserService
, createDockerfile
:FROM openjdk:17-jdk-slim VOLUME /tmp ARG JAR_FILE=target/user-service-0.0.1-SNAPSHOT.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
- Adjust
JAR_FILE
if your artifact name differs.
- In the root directory of
-
Add a Dockerfile to
OrderService
.- In
OrderService
root:FROM openjdk:17-jdk-slim VOLUME /tmp ARG JAR_FILE=target/order-service-0.0.1-SNAPSHOT.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
- Same pattern as
UserService
.
- In
-
Build JAR files for both services.
- In each service folder:
./mvnw clean package
- Ensure
target/user-service-0.0.1-SNAPSHOT.jar
andtarget/order-service-0.0.1-SNAPSHOT.jar
exist.
- In each service folder:
-
Build the Docker image for
UserService
.- From the
UserService
directory:docker build -t user-service:1.0 .
- The
-t
flag names and tags the image (user-service:1.0
).
- From the
-
Build the Docker image for
OrderService
.- From
OrderService
:docker build -t order-service:1.0 .
- From
-
Verify Docker images.
- Check local images:
docker images
- You should see
user-service
andorder-service
with tag1.0
.
- Check local images:
-
Run the
UserService
container.- Start a detached container:
docker run -d --name user-service -p 8081:8081 user-service:1.0
-p 8081:8081
maps host port 8081 to the container’s port 8081.
- Start a detached container:
-
Run the
OrderService
container.- Similarly:
docker run -d --name order-service -p 8082:8082 order-service:1.0
- Similarly:
-
Verify running containers.
- List containers:
docker ps
- Both
user-service
andorder-service
should be running.
- List containers:
-
Test the microservices.
UserService
:http://localhost:8081/users
OrderService
:http://localhost:8082/orders
- Confirm they respond as before.
-
Stop the containers.
- e.g.:
docker stop user-service docker stop order-service
- The containers are no longer running but still exist.
- e.g.:
-
Restart the containers.
docker start user-service
docker start order-service
-
Remove containers.
- To remove them entirely:
docker rm user-service docker rm order-service
- To remove them entirely:
-
Remove Docker images.
- If you no longer need them locally:
docker rmi user-service:1.0 docker rmi order-service:1.0
- If you no longer need them locally:
-
Tag the images for Docker Hub (or another registry).
- For
UserService
:docker tag user-service:1.0 <your-dockerhub-username>/user-service:1.0
- For
OrderService
:docker tag order-service:1.0 <your-dockerhub-username>/order-service:1.0
- For
-
Log in to Docker Hub.
docker login
- Enter your Docker Hub credentials.
-
Push the images to Docker Hub.
- For
UserService
:docker push <your-dockerhub-username>/user-service:1.0
- For
OrderService
:docker push <your-dockerhub-username>/order-service:1.0
- For
-
Verify images on Docker Hub.
- In your Docker Hub account, you should see both
user-service
andorder-service
repos with1.0
tags.
- In your Docker Hub account, you should see both
-
Docker Compose for Multi-Container Deployment.
- Write a
docker-compose.yml
that runs both services together. - e.g.:
version: "3" services: user-service: image: user-service:1.0 ports: - "8081:8081" order-service: image: order-service:1.0 ports: - "8082:8082"
- Write a
-
Environment Variable Configuration.
- Modify
Dockerfile
ordocker-compose.yml
to pass environment variables (e.g., DB credentials).
- Modify
-
Integrate with CI/CD.
- Extend Jenkins or another CI pipeline to build and push these images automatically.
You have successfully:
- Containerized your Spring Boot 3.4.1 microservices (
UserService
&OrderService
) with Docker. - Built Docker images locally and ran the containers.
- Managed container lifecycle (start, stop, remove) and optional pushed images to Docker Hub.
This approach ensures consistent and portable deployments across dev, test, and production environments!