Using Liquibase via Docker

July 29, 2020
Using Liquibase via Docker

Docker is super awesome for running machines that we previously used via virtual machines. But, each virtual machine requires a separate copy of the operating system. That’s a lot of space. Docker leverages the host system for the operating system to cut down on space. Docker also makes it really fast to start those machines.

We can easily start a PostgreSQL database using docker run postgres. This looks at the local system to see if the PostgreSQL Docker container already exists. If not, it will go to Docker Hub and download it. Neat!

We can also do the same for utilities like Liquibase. The only difference is that the Liquibase Docker container will run Liquibase and exit. Unlike a long-running server process, like PostgreSQL, it doesn’t need to be running all the time.

The value of this is that you might need to run Liquibase from a system that does not have Liquibase installed but does have Docker. Here’s the documentation.

Quick example of using Liquibase via Docker with PostgreSQL

Here’s a quick example of how to use it with PostgreSQL.

Your Liquibase changelog needs to be accessible to the Docker container. So, we’ll mount the directory that holds that file with the -v option. Here’s the command:

docker run -v /home/r2/changelog:/liquibase/changelog liquibase/liquibase --driver=org.postgresql.Driver --url=”jdbc:postgresql://<DATABASE_IP>:5432/postgres” --changeLogFile=/liquibase/changelog/changelog.xml --username=postgres --password=postgres

The only two things you will need to change:

  • Update the /home/r2/changelog directory to the directory that contains your changelog
  • Enter the hostname/ip of your database

Bonus fun: If you are running PostgreSQL as a docker container, use docker inspect <CONTAINER_NAME> to find the IP address of your PostgreSQL database.

Let us know if you use Liquibase with Docker and run into any issues. Here’s where to find us.

Article author
Robert Reeves Co-founder and CTO