|
| 1 | +--- |
| 2 | +title: "Customizing the docker build bake hcl file" |
| 3 | +date: 2025-08-22 |
| 4 | +draft: true |
| 5 | +image: |
| 6 | + url: elephant_cookie.jpg |
| 7 | + attribution: https://www.wallpaperflare.com/cookies-elephant-breakfast-for-children-dessert-food-sweet-food-wallpaper-asujf/download |
| 8 | +authors: |
| 9 | + - dchambre |
| 10 | +tags: |
| 11 | + - blog |
| 12 | + - information |
| 13 | + - programming |
| 14 | + - applications |
| 15 | + - containers |
| 16 | + - postgresql |
| 17 | + - postgres |
| 18 | + - images |
| 19 | + - tutorial |
| 20 | + - bake |
| 21 | + - docker |
| 22 | +summary: Jonathan Gonzalez wrote a guide on this blog detailing how to customize Docker images by using an override hcl file. I tried it for a spin. |
| 23 | + |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Summary |
| 28 | + |
| 29 | +The other week [Jonathan Gonzalez]({{% ref "/authors/jgonzalez/" %}}) wrote an |
| 30 | +article on |
| 31 | +[how to customize docker images using an override hcl file]({{% ref "/blog/building-images-bake/" %}}). |
| 32 | +Before the [postgres-containers repo](https://github.com/cloudnative-pg/postgres-containers) |
| 33 | +was enhanced with the option to build the images with `docker build bake`, |
| 34 | +I had to follow these steps manually in order to have custom images for our workloads. |
| 35 | + |
| 36 | +- clone the repo |
| 37 | +- edit the dockerfile |
| 38 | +- build the image |
| 39 | +- push it to the registry |
| 40 | + |
| 41 | +Edit, build and push had to be done for each PostgreSQL version. |
| 42 | +So a lot of boring work needed to be done in order to have updated images. |
| 43 | +The chance to avoid this work sounded promising to me, so I started with the |
| 44 | +[hcl file](https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg.github.io/refs/heads/main/content/blog/building-images-bake/bake.hcl) |
| 45 | +Jonathan wrote, and adapted it to fit my needs. |
| 46 | +After a troubleshooting session with Jonathan, he asked me to share the changes I made. |
| 47 | +So here are my detailed instructions, in case they could prove useful to others. |
| 48 | + |
| 49 | +## Instructions |
| 50 | + |
| 51 | +### Step 1: Prepare the local Bake file |
| 52 | + |
| 53 | +To build a custom image we add the following content in a local file with name |
| 54 | +`bake.hcl`: |
| 55 | + |
| 56 | +```hcl |
| 57 | +platforms = [ |
| 58 | + "linux/amd64", |
| 59 | +] |
| 60 | +
|
| 61 | +extensions = [ |
| 62 | + "dbgsym", |
| 63 | + "partman", |
| 64 | + "oracle-fdw", |
| 65 | + "squeeze", |
| 66 | + "show-plans", |
| 67 | + "cron", |
| 68 | + "tds-fdw", |
| 69 | +] |
| 70 | +
|
| 71 | +target "myimage" { |
| 72 | + dockerfile-inline = <<EOT |
| 73 | +ARG BASE_IMAGE="ghcr.io/cloudnative-pg/postgresql:16.9-standard-bookworm" |
| 74 | +FROM $BASE_IMAGE AS myimage |
| 75 | +ARG EXTENSIONS |
| 76 | +USER root |
| 77 | +RUN apt-get update && \ |
| 78 | + apt-get install -y --no-install-recommends $EXTENSIONS \ |
| 79 | + ldap-utils \ |
| 80 | + ca-certificates \ |
| 81 | + openssl \ |
| 82 | + procps \ |
| 83 | + postgresql-plpython3-"${getMajor(pgVersion)}" \ |
| 84 | + python3-psutil \ |
| 85 | + pgtop \ |
| 86 | + pg-activity \ |
| 87 | + nmon \ |
| 88 | + libsybdb5 \ |
| 89 | + freetds-dev \ |
| 90 | + freetds-common && \ |
| 91 | + apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \ |
| 92 | + rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/* |
| 93 | +RUN sed -i -e 's/# de_AT.UTF-8 UTF-8/de_AT.UTF-8 UTF-8/' /etc/locale.gen && \ |
| 94 | + locale-gen |
| 95 | +ADD https://your.git.url/postgresql/-/blob/main/.psqlrc?ref_type=heads /var/lib/postgresql/ |
| 96 | +ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/etc/ldap/ldap.conf?ref_type=heads /etc/ldap/ |
| 97 | +ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/usr/local/share/ca-certificates/EuropeanSSLServerCA2.crt?ref_type=heads /usr/local/share/ca-certificates/ |
| 98 | +ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/usr/local/share/ca-certificates/RootCA1v0.crt?ref_type=heads /usr/local/share/ca-certificates/ |
| 99 | +ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/usr/local/share/ca-certificates/SubCA1v1.crt?ref_type=heads /usr/local/share/ca-certificates/ |
| 100 | +RUN update-ca-certificates |
| 101 | +USER 26 |
| 102 | +EOT |
| 103 | + matrix = { |
| 104 | + tgt = [ |
| 105 | + "myimage" |
| 106 | + ] |
| 107 | + pgVersion = [ |
| 108 | + "13.21", |
| 109 | + "14.18", |
| 110 | + "15.13", |
| 111 | + "16.9", |
| 112 | + "17.5", |
| 113 | + ] |
| 114 | + } |
| 115 | + name = "postgresql-${index(split(".",cleanVersion(pgVersion)),0)}-standard-bookworm" |
| 116 | + target = "${tgt}" |
| 117 | + args = { |
| 118 | + BASE_IMAGE = "ghcr.io/cloudnative-pg/postgresql:${cleanVersion(pgVersion)}-standard-bookworm", |
| 119 | + EXTENSIONS = "${getExtensionsString(pgVersion, extensions)}", |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +Starting at the beginning of the file: |
| 125 | + |
| 126 | +- The `platforms` variable is `linux/amd64` for all of my images. |
| 127 | +- The `extensions` variable contains some extensions I use regularly. |
| 128 | +- The `dockerfile-inline` part is extended with binaries, some of them are handy |
| 129 | + to have, some needed by extensions or other tools I use e.g. [pgwatch](https://github.com/cybertec-postgresql/pgwatch). |
| 130 | +- With the `sed` command I add needed locales and build them. |
| 131 | +- With the `ADD` commands I extend the image with |
| 132 | + - .psqlrc file, to have a nice psql Command-line even when connecting via |
| 133 | + `kubectl cnpg psql XXX` |
| 134 | + - ldap.conf and the needed certs |
| 135 | + |
| 136 | +### Step 2: Build the image |
| 137 | + |
| 138 | +We can now build the image using the following command: |
| 139 | + |
| 140 | +```bash |
| 141 | +environment=production registry=your.repo.url docker buildx bake -f docker-bake.hcl -f cwd://bake.hcl "https://github.com/cloudnative-pg/postgres-containers.git" myimage |
| 142 | +``` |
| 143 | + |
| 144 | +- The `environment` variable is set to `production` for all of my images, |
| 145 | +because I use the same image to stage it through dev/test/prod. |
| 146 | +- The `registry` variable contains the repo upload url, so the images get |
| 147 | +uploaded there instead of the `localhost:5000` registry used in the |
| 148 | +[hcl file](https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg.github.io/refs/heads/main/content/blog/building-images-bake/bake.hcl). |
| 149 | + |
| 150 | +### Step 3: Use it |
| 151 | + |
| 152 | +The only missing step to use the images is to update your |
| 153 | +[Image Catalog / Cluster Image Catalog](https://cloudnative-pg.io/documentation/current/image_catalog/) |
| 154 | +with the newly built images. |
| 155 | +Test them and stage them through your environment. |
| 156 | + |
| 157 | +## Conclusion |
| 158 | + |
| 159 | +Once you prepare the override file to fit to your needs, the only manual steps |
| 160 | +to build new images are |
| 161 | + |
| 162 | +- udpate the `pgVersion` variable |
| 163 | +- run the `docker buildx bake` command |
| 164 | + |
| 165 | +I hope this helps streamline your image customization process as much as it |
| 166 | +did mine—feel free to build on it, and share your own improvements too! |
| 167 | +You can find the supportive team in the CloudNativePG channels on the CNCF Slack workspace. |
0 commit comments