Example repository to set up a Postgresql database image with a default database.
The project consists of three parts, the Dockerfile
to create the Docker image and the init.sql
to set up the database and a release.yml
to publish the Docker image to Docker Hub.
Conveniently the official postgres image has support for initialization scripts. That means we only have to copy the *.sql
scripts that we want to the correct directory and this will automatically be called on first run.
We set the default user/password for the superuser using environment variables. These can be overwritten by passing the environment variables on first run, e.g. docker run -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password
.
The initialization script (init.sql
) is called on the first run of the image. Here we can create users/databases/tables and insert dummy data. It is recommended to create a different user here that, is not a superuser.
The release.yml
workflow, is triggered by either creating a release or by pushing a version tag to GitHub, e.g.
git tag v0.1.0 -m "Initial version"
git push origin v0.1.0
The workflow will then get a copy of this repository and set up the Docker environment to build the image for multiple platforms (arm64
and amd64
). It will then build the images and push these to Docker Hub using the credentials stored in the secrets.