The reason I wanted a prebuilt database image was how long the tests were taking in CI. I wanted the test job to start with a migrated schema, then give each test its own copy instead of replaying migrations or sharing an application database.
Postgres already has the copying part. With a migrated database named app, a test can start from:
CREATE DATABASE t_case_42 TEMPLATE app;
The part I needed to build was an image that arrived with app ready to copy. That moves the schema setup into the image build, where it can be reused by consuming projects.
Run initialization during the image build
The official Postgres image includes docker-ensure-initdb.sh. It initializes an empty data directory, starts a temporary server, runs the initialization scripts, and shuts the temporary server down.
Calling it in a Dockerfile RUN step lets an init script run goose up during the build. The resulting cluster becomes part of the image. Marking the migrated application database as a template then gives the tests a source to copy.
This changes when configuration takes effect. The official image’s initialization variables apply to an empty data directory. Once the cluster is baked, setting a different POSTGRES_DB or POSTGRES_PASSWORD when the container starts doesn’t recreate it. The schema, initial roles, and authentication setup have to be deliberate build inputs. The image documentation calls out this behavior.
Keep the data directory in the image
I put PGDATA at /var/lib/postgresql/baked-data, outside the volume declared by the Postgres 16 and 17 images at /var/lib/postgresql/data.
There is an important builder distinction here. With Docker’s legacy builder, changes made under a declared volume by later build steps are discarded. BuildKit keeps those changes. A VOLUME declaration alone doesn’t explain why a baked cluster disappeared. Docker documents both behaviors.
The path above is also specific to the older image layout. Postgres 18 and later declare the volume at /var/lib/postgresql, so baked-data would be inside it. Check the base image’s layout when changing major versions. And don’t mount a different data directory over the cluster you meant to use. The official image documents the version 18 change.
Check readiness without connecting to the template
The official entrypoint starts its temporary initialization server with listen_addresses='', so it only accepts Unix-socket connections. A socket healthcheck can report success while initialization is still running. A TCP check avoids that particular false positive. The startup code makes the distinction explicit.
pg_isready -h 127.0.0.1 -d postgres
The check targets postgres, not app. Postgres won’t copy a template while another session is connected to it, so healthchecks and administrative connections should stay away from the source database. Tests should connect to their own copies. The template database documentation explains the connection restriction and required privileges.
A readiness check reports whether the server is accepting connections. It doesn’t verify successful authentication, the target database’s existence, or the schema. Keep a build-time check that the expected migrations completed too.
Give migrations an explicit environment
Some of the migrations use environment substitution. That makes inherited build variables part of the input unless they’re controlled.
Running Goose through env -i starts with a clean environment. Pass the values the migration runner needs explicitly, including any required path or connection settings. With Goose’s substitution enabled, ${VAR?message} can then reject an unset variable instead of accidentally picking up a value from the surrounding build. An empty but defined value still needs a separate check if it isn’t valid. Goose documents the annotation and supported expansions.
Use test-only values. A baked database is an image artifact, so real credentials or private records don’t belong in it.
Keep passwordless test databases isolated
The test setup uses passwordless roles, which makes POSTGRES_HOST_AUTH_METHOD=trust convenient. It also means anyone who can reach a matching connection rule can claim a database user name, including a superuser. Postgres is explicit about that risk.
If a job on the host needs a published port, bind it to loopback rather than all interfaces, for example 127.0.0.1::5432. That limits the published host port under Docker’s normal bridge setup. It doesn’t protect against other processes on the host or containers on the same network. Older Docker releases also have a localhost-publishing caveat. Docker’s port-publishing documentation covers those boundaries.
This belongs in a disposable, trusted test environment. Loopback binding by itself doesn’t make trust safe.
Keep the schema and the tests in step
A prebuilt schema adds one more version to coordinate. The consuming job should use an image tied to the migration revision it expects, ideally pinned by digest. Otherwise a fast startup can still give the tests the wrong database.
Keep migration testing as a separate path too. Tests against a fully migrated image exercise the resulting schema. They don’t prove that an existing database can upgrade through the migrations successfully.
I haven’t run the image as a service in a consuming project’s CI yet, so the end-to-end time saving is still unmeasured. The useful result so far is the build pattern: do the migration work once per image, preserve that cluster, and clone the migrated database for isolated tests. The next measurement needs to include the image pull and startup, not just the database copy.
Sources
- Postgres image documentation, build-time initialization script, and entrypoint.
- Docker volume behavior and port publishing.
- Postgres template databases,
pg_isready, and trust authentication. - Goose environment substitution.
I’d appreciate a follow. You can subscribe with your email below. The emails go out once a week, or you can find me on Mastodon at @[email protected].