Running a Database
In the previous lesson, we explored the structure of our Spring Boot blog application.
Now we'll set up a PostgreSQL database using Docker Compose, which will serve as the persistent data store for our blog.
This containerized approach ensures consistency across development environments and simplifies database management.
Understanding Docker Compose
Docker Compose is a tool that defines and manages multi-container Docker environments.
For our blog platform, we need a PostgreSQL database server and (optionally) a database management interface.
Docker Compose allows us to define these services in a declarative way using a YAML file.
Creating the Docker Compose File
Let's create a new file named docker-compose.yml in the project root directory with the following content:
services:
# Our PostgreSQL database
db:
# Using the latest PostgreSQL image
image: postgres:latest
ports:
- '5432:5432'
restart: always
environment:
POSTGRES_PASSWORD: changemeinprod!
# Database management interface
adminer:
image: adminer:latest
restart: always
ports:
- 8888:8080Running the Database
Starting the database requires Docker to be installed and running on your system.
Open a terminal in your project directory and run:
docker-compose upThis command starts both the PostgreSQL database and Adminer.
Accessing the Database
The PostgreSQL database is now accessible on port 5432.
You can access the Adminer interface at http://localhost:8888 with these credentials:
- System:
PostgreSQL - Server:
db - Username:
postgres - Password:
changemeinprod!
Summary
- Docker Compose enables consistent database setup across development environments
- PostgreSQL runs in a container, avoiding direct installation on the host system
- Adminer provides a web interface for database management
- The database automatically restarts if the Docker daemon restarts