Creating a Custom PostgreSQL Docker Image with Initialization Scripts

Containerisation has become a common practice of modern software development after its introduction a decade ago. It allowed developers to package applications together with their dependencies into reproducible, isolated units. Databases are no exception — and PostgreSQL, with its stability and rich SQL features, is a go-to choice for many.

In this article, we’ll walk through how to create a custom PostgreSQL Docker image that automatically initialises your schema and loads snapshot data when the container starts for the first time.

If you haven’t seen it yet, you might also want to read Creating a Custom MySQL Docker Image with Initialization Scripts — the process is quite similar, but there are a few PostgreSQL-specific nuances we’ll cover here.

Here is how we are going to do it step by step:

  1. Create a Dockerfile that extends the official PostgreSQL image
  2. Add your SQL initialization scripts
  3. Build and run the image with a new database instance
  4. Verify that the database and data are loaded correctly
Continue reading “Creating a Custom PostgreSQL Docker Image with Initialization Scripts”

Dockerized PostgreSQL/MySQL with Snapshot Data for Spring Boot (JPA)

Guide to Spring Boot (JPA) using Dockerized PostgreSQL/MySQL with snapshot data. Add init scripts, profiles, and quick tests for faster development.
Spring Boot JPA with dockerized snapshot data

This guide demonstrates a Spring Boot + Spring Data JPA setup powered by Docker database images seeded with initial data, simplifying local development and staging.

The core dependencies of the example are as follows:

  • Spring Boot 2.5.0
  • Spring 5.3.7
  • Hibernate 5.4.31.Final
  • PostgreSQL driver 42.2.20
  • MySQL connector 8.0.25 (Alternative Database Option)

We are going to follow the listed steps throughout this example:

  1. Introduce PostgreSQL database as the default database for the application
  2. Create and run a PostgreSQL Docker image backed by initial data
  3. Add entities and repositories to the application
  4. Test the initial setup
  5. Introduce MySQL database as a secondary database option to the application
  6. Create and run a MySQL Docker image backed by initial data
Continue reading “Dockerized PostgreSQL/MySQL with Snapshot Data for Spring Boot (JPA)”