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

breaking down the docker compose

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)
2 views3 pages

breaking down the docker compose

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
You are on page 1/ 3

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 docker-compose.yml 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 http://localhost:8080 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

You might also like