0% found this document useful (0 votes)
59 views3 pages

Docker Compose for Spring Boot & PostgreSQL

The document explains a Docker Compose configuration for running a Spring Boot application and a PostgreSQL database. It details the services, ports, environment variables, volumes, and networks used to facilitate communication between the app and the database. The configuration ensures data persistence and proper service dependencies for successful deployment.

Uploaded by

Yanet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Docker Compose for Spring Boot & PostgreSQL

The document explains a Docker Compose configuration for running a Spring Boot application and a PostgreSQL database. It details the services, ports, environment variables, volumes, and networks used to facilitate communication between the app and the database. The configuration ensures data persistence and proper service dependencies for successful deployment.

Uploaded by

Yanet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Breaking down the docker

compose
1. Version: '3'
This specifies the version of the Docker Compose file format. In this case,
version 3 is used, which is compatible with Docker Engine 1.13.0 and above.

2. Services
The services section defines all the containers you want to run. In this example,
there are two

1. Create a [Link] file without the env variables

Service: app

: This specifies the Docker image for your Spring Boot


image: your-app-name

application. You can either use a locally built image (via docker build ) or pull an
image from a Docker registry (like Docker Hub).

Example: You could build your Spring Boot app image and tag it as your-

app-name before running the Docker Compose file.

: This maps port 8080 on the host machine to port 8080


ports: - "8080:8080"

inside the container. This allows you to access your Spring Boot app by
navigating to [Link] in your browser.

depends_on: - db : This ensures that the app service (Spring Boot app) starts
only after the db service (PostgreSQL) is up and running. This ensures that
the database is available before the app starts.

: This sets environment variables inside the app container that


environment

Spring Boot will use to connect to the PostgreSQL database.

SPRING_DATASOURCE_URL : The JDBC connection URL pointing to the db service


(which is the PostgreSQL container).

and SPRING_DATASOURCE_PASSWORD : The username and


SPRING_DATASOURCE_USERNAME

password for connecting to the PostgreSQL database.

Breaking down the docker compose 1


: This tells Docker Compose to connect the app service to
networks: - my-network

the custom network my-network . This allows the app and db services to
communicate with each other.

Service: db

image: postgres:17 : This specifies the Docker image to use for the PostgreSQL
database container. In this case, it's using the official postgres image, version
17.

environment : These environment variables are used to initialize the PostgreSQL


container.

: This sets the name of the default database that


POSTGRES_DB=mydatabase

PostgreSQL will create upon container startup.

POSTGRES_USER=amigoscode : The username for accessing the PostgreSQL


database.

POSTGRES_PASSWORD=password : The password for the amigoscode user.

volumes: - postgres-data:/var/lib/postgresql/data: This mounts the Docker volume


postgres-data to the PostgreSQL container's data directory

( /var/lib/postgresql/data ). This ensures that the data is persisted even if the


container is stopped or removed.

networks: - my-network : This connects the db service to the same custom


network my-network as the app service, enabling communication between
them.

3. Networks
: This defines a custom network called my-network . Both the app and db
my-network

services are connected to this network, allowing them to communicate with each
other using the service names ( app and db ) as hostnames.

4. Volumes

: This creates a named volume for persisting PostgreSQL data.


postgres-data

Docker volumes are used to persist data outside the lifecycle of a container. The

Breaking down the docker compose 2


volume postgres-data will store PostgreSQL data in the host system, so even if the
container is removed, the data is preserved and can be reused.

How the Services Communicate


App to DB: The Spring Boot app connects to the PostgreSQL database using
the SPRING_DATASOURCE_URL defined in the environment variables. Since both
services are on the same custom network ( my-network ), the app can access the
database container using the service name db as the hostname.

The JDBC URL: jdbc:postgresql://db:5432/mydatabase means that the app will


connect to the db service (PostgreSQL) on port 5432, using the mydatabase
database.

Networking: The custom network ( my-network ) ensures that the two services
can resolve each other's hostnames ( app and db ) and communicate internally.

💡 Summary
Services: Defines two containers: one for the Spring Boot
application and one for PostgreSQL.

Ports: Maps port 8080 of the host to port 8080 of the Spring Boot
app container, enabling access via localhost:8080 .

Environment Variables: Used for database credentials and


configuration inside the containers.

Volumes: Persist PostgreSQL data using a Docker volume ( postgres-


data ).

Networks: Both services are connected to the same custom network


( my-network ) for communication between them.

Breaking down the docker compose 3

Common questions

Powered by AI

Docker volumes play a critical role in maintaining data integrity and persistence by ensuring that data remains intact across container restarts and removals. In the outlined setup, a named volume 'postgres-data' is used for persisting PostgreSQL data. This volume is mounted to the PostgreSQL container's data directory, meaning that database data is stored on the host system. Consequently, even if the PostgreSQL container is stopped or deleted, the actual data in 'postgres-data' remains unaffected, ready to be reused in subsequent deployments .

Network isolation and management using a custom network, such as 'my-network', in Docker Compose configurations enhance security and communication efficiency. By connecting services to a dedicated network with specific configurations, you ensure that only the intended services can communicate, reducing unintended exposure to other network entities. This approach also simplifies hostname resolution between connected services, allowing the app to connect to the database service seamlessly using its service name, thus optimizing internal communications and enhancing overall operational security .

Mapping containers to persistent named volumes is crucial for data continuity, as it separates data storage from the container lifecycle. In Docker Compose, this setup enables applications like PostgreSQL to maintain their data across container restarts or removals. This permanence is vital for retaining the state and history of applications, supporting robust backup policies, and facilitating smooth upgrades or migrations by maintaining data integrity independent of container operations .

The 'depends_on' keyword in Docker Compose dictates the startup sequence of services by ensuring that certain services are initiated only after others are ready. In the provided scenario, the 'depends_on' keyword is used to ensure that the app service (Spring Boot application) starts only after the db service (PostgreSQL) is running. This is crucial because the app service requires the database to be accessible for initial setup and operations, avoiding connection failures at startup .

Specifying the image version, such as 'postgres:17' for the db service, ensures consistency and reliability by controlling the exact environment in which the services run. It avoids unpredictability associated with using the latest version, which may introduce breaking changes or incompatibility with other components like the app service. This specification helps maintain stable deployments and consistent behavior across development, testing, and production environments .

Using a locally built image for the Spring Boot application allows developers to incorporate recent changes and customizations more rapidly than waiting for an image to be available on a registry. However, it can lead to inconsistencies in larger teams or across environments if not managed properly. Conversely, pulling an image from a Docker registry like Docker Hub provides a reliable and consistent base but may lack the latest updates or custom configurations unless frequently updated. Balancing these strategies can optimize both development flexibility and operational consistency .

The environment variables in the Docker Compose setup are crucial for configuring the database connection and ensuring that the application functions correctly. The variables SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD define the database connection parameters, such as the JDBC URL, username, and password required for the Spring Boot app to connect to the PostgreSQL database. This configuration determines the application's ability to gather, store, and manage data from the database effectively .

While 'depends_on' in Docker Compose helps control service startup order, it does not wait for a service to be "ready" (e.g., for a database to be initialized). This could lead to issues where dependent services, like an app, attempt to connect to a database that isn't fully ready. These challenges can be addressed by implementing health checks or retries within the app code to confirm the database readiness before proceeding with operations, ensuring robustness and reducing startup failures .

Port mapping in Docker Compose facilitates access to the Spring Boot application by mapping a port on the host machine to a port inside the container. In the described setup, port 8080 on the host is mapped to port 8080 of the app container, allowing users to access the app via http://localhost:8080. This configuration is crucial for development and testing by making the containerized application accessible to external requests .

The custom network 'my-network' facilitates communication between the app and database services by allowing them to resolve each other's hostnames and communicate internally within Docker. By being on the same custom network, the app service can access the database service using its service name 'db' as the hostname. This setup ensures seamless communication and networking, with the environment variable SPRING_DATASOURCE_URL using jdbc:postgresql://db:5432/mydatabase to point to the db service .

You might also like