This example demonstrates how to use Liquibase with Docker Compose to manage database changes alongside a PostgreSQL database.
- Docker and Docker Compose installed
- Basic understanding of Liquibase and database migrations
- Start the services:
docker-compose up
-
Start the services with local build:
docker-compose -f docker-compose.local.yml up --build
-
Verify the migration: The Liquibase service will automatically run the
updatecommand after PostgreSQL is ready. Check the logs to see the migration results:docker-compose logs liquibase
-
Connect to the database to verify:
docker-compose exec postgres psql -U liquibase -d liquibase_demo -c "SELECT * FROM users;"
-
Stop the services:
docker-compose down
- PostgreSQL: Runs a PostgreSQL 15 Alpine container with a database named
liquibase_demo - Liquibase: Uses the official Alpine Liquibase image to run database migrations
- Sample Migration: Creates a
userstable and inserts sample data - Health Checks: Ensures PostgreSQL is ready before running Liquibase migrations
docker-compose/
├── docker-compose.yml # Docker Compose with published image
├── docker-compose.local.yml # Docker Compose with local build
├── liquibase.properties # Liquibase configuration
├── changelog/
│ ├── db.changelog-master.xml # Master changelog file
│ ├── 001-create-users-table.xml
│ └── 002-insert-sample-data.xml
└── README.md # This file
The example uses environment variables for database connection:
LIQUIBASE_COMMAND_URL: Database connection URLLIQUIBASE_COMMAND_USERNAME: Database usernameLIQUIBASE_COMMAND_PASSWORD: Database password
./changelog:/liquibase/changelog: Mounts local changelog files./liquibase.properties:/liquibase/liquibase.properties: Mounts configuration filepostgres_data: Persists PostgreSQL data
To run other Liquibase commands, you can override the default command:
# Generate SQL for review
docker-compose run --rm liquibase --defaults-file=/liquibase/liquibase.properties update-sql
# Rollback last changeset
docker-compose run --rm liquibase --defaults-file=/liquibase/liquibase.properties rollback-count 1
# Check status
docker-compose run --rm liquibase --defaults-file=/liquibase/liquibase.properties statusTo adapt this example for your use case:
- Change Database: Modify the
postgresservice indocker-compose.yml - Update Connection: Modify
liquibase.propertieswith your database details - Add Your Migrations: Replace the sample changelog files with your own
- Environment: Adjust environment variables as needed