This simple repository specifies a sample Liquibase project. The instructions below use this project to demonstrate how Liquibase can be run within a Docker container to apply a changeset to a pgsql database.
This sample leverages Docker Compose to simplify the orchestration between Docker containers. It simplifies the steps necessary to execute the example, but is not necessary for running Liquibase within a Docker container. In order to run the example below, you must have the Docker tool versions below:
- Docker Engine 1.13.0+
- Docker Compose 1.17.0+
> docker-compose up -d db
> docker-compose run --rm psql
default=# \dt
No relations found.
default=# create table test (id int);
CREATE TABLE
default=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | test | table | admin
(1 row)
default=# select * from test;
id
----
(0 rows)
default=# \q
> docker-compose run --rm liquibase updateSQL
> docker-compose run --rm liquibase update
> docker-compose run --rm psql
default=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------------------+-------+-------
public | bar | table | admin
public | databasechangelog | table | admin
public | databasechangeloglock | table | admin
public | foo | table | admin
public | test | table | admin
(5 rows)
default=# \q
Step 6 - Run Liquibase container to preview that a subsequent run does not attempt to create the tables again
> docker-compose run --rm liquibase updateSQL
(the update sql should indicate that no changes are required)
> docker-compose down --volumes
To run the Liquibase Docker container against your own Postgres database, substitue the
following environment vars either in your local environment or in the .env
file.
- POSTGRES_HOST-- hostname of the Postgres database server (default=db).
- POSTGRES_PORT-- port number of the Postgres database server (default=5432).
- POSTGRES_USER-- username of the Postgres database user (default=admin).
- POSTGRES_PASSWORD-- password of the Postrgres database user (default=pw).
- POSTGRES_DB-- name of the database on the Postgres database server (default=default)
In order to prevent the test Docker containers from launching, suppress the override file for Docker Compose by specifying the main compose file. As an example
> docker-compose -f docker-compose.yml run --rm liquibase updateSQL