d. Containerization

As we have seen in the previous hands-on, the microservices are implemented as Spring Boot applications. To run the microservices, we need to prepare the environment by installing the database to persist the data. For that, we will use a Docker Compose file to create a PostgreSQL container, as well as, a cluster to isolate the microservices from external access, creating a secure environment - trusted layer.

A Docker Compose file is a YAML file that defines how Docker containers should behave in production. The file contains the configuration for the database, the microservices, and the network configuration.

The bellow diagram illustrates the architecture of the system, that will be created using Docker Compose, where the microservices are isolated from the external access, creating a trusted layer. The microservices can only access the database, and the external access is blocked.

flowchart LR
    subgraph api [Trusted Layer]
        direction TB
        account e3@==> db@{ shape: cyl, label: "Database" }
    end
    internet e1@==>|request| account:::red
    e1@{ animate: true }
    e3@{ animate: true }
    classDef red fill:#fcc

At diagram, the account microservice is the business logic layer that interacts with the database.

The directory structure of the project looks like something as follows:

📁 api/
├── 📁 account/
├── 📁 account-service/
   ├── 📁 src/
   └──  Dockerfile
├──  .env
└──  compose.yaml

The compose.yaml file contains the configuration for the database and the microservices, as well as, the network configuration. The .env file contains the environment variables for the database, such as the username, password, and database name. The Dockerfile file contains the configuration for the microservice, such as the base image, the dependencies, and the command to run the application.

The content of the files are as follows:

name: store

services:

  db:
    image: postgres:17
    ports:
      - 5432:5432
    volumes:
      - ${VOLUME_DB}:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: ${DB_USER:-store}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-devpass}
      POSTGRES_DB: ${DB_NAME:-store}

  account:
    build:
      context: ./account-service
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    depends_on:
      - db
1
2
3
4
DB_USER=store
DB_PASSWORD=5t0r3
DB_NAME=store
VOLUME_DB=./volume/db
1
2
3
4
FROM eclipse-temurin:25
VOLUME /tmp
COPY target/*.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

In this configuration, we use the postgres:17 image to create a PostgreSQL container, and we set the environment variables for the database using the .env file.

To run the application, we need to execute the following command:

docker compose up -d --build

Yet, to check if the application is running, we can execute the following command:

docker compose ps -a

Done! In the next hands-on, we will connect to the database and execute some queries to check if the data is being persisted correctly.

Repository