|  | 
|  | 1 | +# Building PostgreSQL Container Images for CloudNativePG | 
|  | 2 | + | 
|  | 3 | +This guide outlines the process for building PostgreSQL operand images for | 
|  | 4 | +CloudNativePG using [Docker Bake](https://docs.docker.com/build/bake/) and a | 
|  | 5 | +[GitHub workflow](.github/workflows/bake.yaml). | 
|  | 6 | + | 
|  | 7 | +The central component of this framework is the | 
|  | 8 | +[Bake file (`docker-bake.hcl`)](docker-bake.hcl). | 
|  | 9 | + | 
|  | 10 | +## Prerequisites | 
|  | 11 | + | 
|  | 12 | +Ensure the following tools and components are available before proceeding: | 
|  | 13 | + | 
|  | 14 | +1. [Docker Buildx](https://github.com/docker/buildx): A CLI plugin for advanced | 
|  | 15 | +image building. | 
|  | 16 | +2. Build Driver for Multi-Architecture Images: For example, `docker-container` | 
|  | 17 | +(see [Build Drivers](https://docs.docker.com/build/builders/drivers/) and | 
|  | 18 | +["Install QEMU Manually"](https://docs.docker.com/build/building/multi-platform/#install-qemu-manually)). | 
|  | 19 | +3. [Distribution Registry](https://distribution.github.io/distribution/): | 
|  | 20 | +Formerly known as Docker Registry, to host and manage the built images. | 
|  | 21 | + | 
|  | 22 | +### Verifying Requirements | 
|  | 23 | + | 
|  | 24 | +To confirm your environment is properly set up, run: | 
|  | 25 | + | 
|  | 26 | +```bash | 
|  | 27 | +docker buildx bake --check | 
|  | 28 | +``` | 
|  | 29 | + | 
|  | 30 | +If errors appear, you may need to switch to a different build driver. For | 
|  | 31 | +example, use the following commands to configure a `docker-container` build | 
|  | 32 | +driver: | 
|  | 33 | + | 
|  | 34 | +```bash | 
|  | 35 | +docker buildx create \ | 
|  | 36 | +  --name docker-container \ | 
|  | 37 | +  --driver docker-container \ | 
|  | 38 | +  --use \ | 
|  | 39 | +  --driver-opt network=host \ | 
|  | 40 | +  --bootstrap | 
|  | 41 | +``` | 
|  | 42 | + | 
|  | 43 | +> *Note:* The `--driver-opt network=host` setting is required only for testing | 
|  | 44 | +> when you push to a distribution registry listening on `localhost`. | 
|  | 45 | +
 | 
|  | 46 | +> *Note:* This page is not intended to serve as a comprehensive guide for | 
|  | 47 | +> building multi-architecture images with Docker and Bake. If you encounter any | 
|  | 48 | +> issues, please refer to the resources listed above for detailed instructions | 
|  | 49 | +> and troubleshooting. | 
|  | 50 | +
 | 
|  | 51 | +## Default Target | 
|  | 52 | + | 
|  | 53 | +The `default` target in Bake represents a Cartesian product of the following | 
|  | 54 | +dimensions: | 
|  | 55 | + | 
|  | 56 | +- **Base Image** | 
|  | 57 | +- **Type** (e.g. `minimal` or `standard`) | 
|  | 58 | +- **Platforms** | 
|  | 59 | +- **PostgreSQL Versions** | 
|  | 60 | + | 
|  | 61 | +## Building Images | 
|  | 62 | + | 
|  | 63 | +To build PostgreSQL images using the `default` target — that is, for all the | 
|  | 64 | +combinations of base image, format, platforms, and PostgreSQL versions — run: | 
|  | 65 | + | 
|  | 66 | +```bash | 
|  | 67 | +docker buildx bake --push | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +> *Note:* The `--push` flag is required to upload the images to the registry. | 
|  | 71 | +> Without it, the images will remain cached within the builder container, | 
|  | 72 | +> making testing impossible. | 
|  | 73 | +
 | 
|  | 74 | +If you want to limit the build to a specific combination, you can specify the | 
|  | 75 | +target in the `VERSION-TYPE-BASE` format. For example, to build an image for | 
|  | 76 | +PostgreSQL 17 with the `minimal` format on the `bookworm` base image: | 
|  | 77 | + | 
|  | 78 | +```bash | 
|  | 79 | +docker buildx bake --push postgresql-17-minimal-bookworm | 
|  | 80 | +``` | 
|  | 81 | + | 
|  | 82 | +You can also limit the build to a single platform, for example AMD64, with: | 
|  | 83 | + | 
|  | 84 | +```bash | 
|  | 85 | +docker buildx bake --push --set "*.platform=linux/amd64" | 
|  | 86 | +``` | 
|  | 87 | + | 
|  | 88 | +The two can be mixed as well: | 
|  | 89 | + | 
|  | 90 | +```bash | 
|  | 91 | +docker buildx bake --push \ | 
|  | 92 | +  --set "*.platform=linux/amd64" \ | 
|  | 93 | +  postgresql-17-minimal-bookworm | 
|  | 94 | +``` | 
|  | 95 | + | 
|  | 96 | +## The Distribution Registry | 
|  | 97 | + | 
|  | 98 | +The images must be pushed to any registry server that complies with the **OCI | 
|  | 99 | +Distribution Specification**. | 
|  | 100 | + | 
|  | 101 | +By default, the build process assumes a registry server running locally at | 
|  | 102 | +`localhost:5000`. To use a different registry, set the `registry` environment | 
|  | 103 | +variable when executing the `docker` command, as shown: | 
|  | 104 | + | 
|  | 105 | +```bash | 
|  | 106 | +registry=<REGISTRY_URL> docker buildx ... | 
|  | 107 | +``` | 
|  | 108 | + | 
|  | 109 | +## Local Testing | 
|  | 110 | + | 
|  | 111 | +You can test the image-building process locally if you meet the necessary | 
|  | 112 | +[prerequisites](prerequisites). | 
|  | 113 | + | 
|  | 114 | +To do this, you'll need a local registry server. If you don't already have one, | 
|  | 115 | +you can deploy a temporary, disposable [distribution registry](https://distribution.github.io/distribution/about/deploying/) | 
|  | 116 | +with the following command: | 
|  | 117 | + | 
|  | 118 | +```bash | 
|  | 119 | +docker run -d --rm -p 5000:5000 --name registry registry:2 | 
|  | 120 | +``` | 
|  | 121 | + | 
|  | 122 | +This command runs a lightweight, temporary instance of the `registry:2` | 
|  | 123 | +container on port `5000`. | 
|  | 124 | + | 
|  | 125 | +## Trademarks | 
|  | 126 | + | 
|  | 127 | +*[Postgres, PostgreSQL and the Slonik Logo](https://www.postgresql.org/about/policies/trademarks/) | 
|  | 128 | +are trademarks or registered trademarks of the PostgreSQL Community Association | 
|  | 129 | +of Canada, and used with their permission.* | 
0 commit comments