-
Notifications
You must be signed in to change notification settings - Fork 29
Blog: customizing the docker build bake hcl file #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+239
−0
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
453ea3f
docs: customice hcl file
smiyc d4e7b01
rename directory and add summary
smiyc f5bb741
pgvector is not needed in the hcl, since is defined in the original h…
smiyc 7076d54
minor change
smiyc 846168f
add image
smiyc a4e4c51
minor changes
smiyc 87242a9
example tools
smiyc 917dabb
Update content/blog/customizing-hcl/index.md
smiyc 1c4d12d
Update content/blog/customizing-hcl/index.md
smiyc 57b86d4
Update content/blog/customizing-hcl/index.md
smiyc c388904
more context
smiyc 078791f
col80
smiyc 0d00386
Update content/blog/customizing-hcl/index.md
smiyc 72ba46f
Update content/blog/customizing-hcl/index.md
smiyc 868ac84
Update content/blog/customizing-hcl/index.md
smiyc 3806a39
Update content/blog/customizing-hcl/index.md
smiyc 2e21542
Update content/blog/customizing-hcl/index.md
smiyc 5a24ba7
Update content/blog/customizing-hcl/index.md
smiyc 19edf8c
update with suggestions
smiyc eb4a8f4
Merge branch 'my_baking_recepie' of https://github.com/smiyc/cloudnat…
smiyc 59e9397
update and remove package-lock.json
smiyc dff8111
Apply suggestions from code review
FloorD 38fec5e
more context
smiyc 3c7d7b2
remove spaces and bring back a missing line
smiyc 40702d9
add blank lines
smiyc 17c5d6e
move environmen®istry variables from the hcl file to the bake command
smiyc fb5a83d
straightened enumerations
smiyc 45706cf
Update content/authors/dchambre/index.md
smiyc 4f3dba7
Update content/blog/customizing-hcl/index.md
jsilvela 49a7fec
Merge branch 'cloudnative-pg:main' into my_baking_recepie
smiyc a0e2430
Update index.md
FloorD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| title: Daniel Chambre | ||
| avatar: daniel.jpg | ||
| github: smiyc | ||
| --- | ||
|
|
||
| A DBA and Open Source enthusiast. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| platforms = [ | ||
| "linux/amd64", | ||
| ] | ||
|
|
||
| extensions = [ | ||
| "dbgsym", | ||
| "partman", | ||
| "oracle-fdw", | ||
| "squeeze", | ||
| "show-plans", | ||
| "cron", | ||
| "tds-fdw", | ||
| ] | ||
|
|
||
| target "myimage" { | ||
| dockerfile-inline = <<EOT | ||
| ARG BASE_IMAGE="ghcr.io/cloudnative-pg/postgresql:16.9-standard-bookworm" | ||
| FROM $BASE_IMAGE AS myimage | ||
| ARG EXTENSIONS | ||
| USER root | ||
| RUN apt-get update && \ | ||
| apt-get install -y --no-install-recommends $EXTENSIONS \ | ||
| ldap-utils \ | ||
| ca-certificates \ | ||
| openssl \ | ||
| procps \ | ||
| postgresql-plpython3-"${getMajor(pgVersion)}" \ | ||
| python3-psutil \ | ||
| pgtop \ | ||
| pg-activity \ | ||
| nmon \ | ||
| libsybdb5 \ | ||
| freetds-dev \ | ||
| freetds-common && \ | ||
| apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \ | ||
| rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/* | ||
| RUN sed -i -e 's/# de_AT.UTF-8 UTF-8/de_AT.UTF-8 UTF-8/' /etc/locale.gen && \ | ||
| locale-gen | ||
| ADD https://your.git.url/postgresql/-/blob/main/.psqlrc?ref_type=heads /var/lib/postgresql/ | ||
| ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/etc/ldap/ldap.conf?ref_type=heads /etc/ldap/ | ||
| 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/ | ||
| 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/ | ||
| 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/ | ||
| RUN update-ca-certificates | ||
| USER 26 | ||
| EOT | ||
| matrix = { | ||
| tgt = [ | ||
| "myimage" | ||
| ] | ||
| pgVersion = [ | ||
| "13.21", | ||
| "14.18", | ||
| "15.13", | ||
| "16.9", | ||
| "17.5", | ||
| ] | ||
| } | ||
| name = "postgresql-${index(split(".",cleanVersion(pgVersion)),0)}-standard-bookworm" | ||
| target = "${tgt}" | ||
| args = { | ||
| BASE_IMAGE = "ghcr.io/cloudnative-pg/postgresql:${cleanVersion(pgVersion)}-standard-bookworm", | ||
| EXTENSIONS = "${getExtensionsString(pgVersion, extensions)}", | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| --- | ||
| title: "Customizing the docker build bake hcl file" | ||
| date: 2025-08-22 | ||
| draft: true | ||
| image: | ||
| url: elephant_cookie.jpg | ||
| attribution: https://www.wallpaperflare.com/cookies-elephant-breakfast-for-children-dessert-food-sweet-food-wallpaper-asujf/download | ||
| authors: | ||
| - dchambre | ||
| tags: | ||
| - blog | ||
| - information | ||
| - programming | ||
| - applications | ||
| - containers | ||
| - postgresql | ||
| - postgres | ||
| - images | ||
| - tutorial | ||
| - bake | ||
| - docker | ||
| 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. | ||
|
|
||
|
|
||
| --- | ||
|
|
||
| ## Summary | ||
|
|
||
| The other week [Jonathan Gonzalez]({{% ref "/authors/jgonzalez/" %}}) wrote an | ||
| article on | ||
| [how to customize docker images using an override hcl file]({{% ref "/blog/building-images-bake/" %}}). | ||
| Before the [postgres-containers repo](https://github.com/cloudnative-pg/postgres-containers) | ||
| was enhanced with the option to build the images with `docker build bake`, | ||
| I had to follow these steps manually in order to have custom images for our workloads. | ||
|
|
||
| - clone the repo | ||
| - edit the dockerfile | ||
| - build the image | ||
| - push it to the registry | ||
|
|
||
| Edit, build and push had to be done for each PostgreSQL version. | ||
| So a lot of boring work needed to be done in order to have updated images. | ||
| The chance to avoid this work sounded promising to me, so I started with the | ||
| [hcl file](https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg.github.io/refs/heads/main/content/blog/building-images-bake/bake.hcl) | ||
| Jonathan wrote, and adapted it to fit my needs. | ||
| After a troubleshooting session with Jonathan, he asked me to share the changes I made. | ||
| So here are my detailed instructions, in case they could prove useful to others. | ||
|
|
||
| ## Instructions | ||
|
|
||
| ### Step 1: Prepare the local Bake file | ||
|
|
||
| To build a custom image we add the following content in a local file with name | ||
| `bake.hcl`: | ||
|
|
||
| ```hcl | ||
| platforms = [ | ||
| "linux/amd64", | ||
| ] | ||
|
|
||
| extensions = [ | ||
| "dbgsym", | ||
| "partman", | ||
| "oracle-fdw", | ||
| "squeeze", | ||
| "show-plans", | ||
| "cron", | ||
| "tds-fdw", | ||
| ] | ||
|
|
||
| target "myimage" { | ||
| dockerfile-inline = <<EOT | ||
| ARG BASE_IMAGE="ghcr.io/cloudnative-pg/postgresql:16.9-standard-bookworm" | ||
| FROM $BASE_IMAGE AS myimage | ||
| ARG EXTENSIONS | ||
| USER root | ||
| RUN apt-get update && \ | ||
| apt-get install -y --no-install-recommends $EXTENSIONS \ | ||
| ldap-utils \ | ||
| ca-certificates \ | ||
| openssl \ | ||
| procps \ | ||
| postgresql-plpython3-"${getMajor(pgVersion)}" \ | ||
| python3-psutil \ | ||
| pgtop \ | ||
| pg-activity \ | ||
| nmon \ | ||
| libsybdb5 \ | ||
| freetds-dev \ | ||
| freetds-common && \ | ||
| apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \ | ||
| rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/* | ||
| RUN sed -i -e 's/# de_AT.UTF-8 UTF-8/de_AT.UTF-8 UTF-8/' /etc/locale.gen && \ | ||
| locale-gen | ||
| ADD https://your.git.url/postgresql/-/blob/main/.psqlrc?ref_type=heads /var/lib/postgresql/ | ||
| ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/etc/ldap/ldap.conf?ref_type=heads /etc/ldap/ | ||
| 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/ | ||
| 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/ | ||
| 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/ | ||
| RUN update-ca-certificates | ||
| USER 26 | ||
| EOT | ||
| matrix = { | ||
| tgt = [ | ||
| "myimage" | ||
| ] | ||
| pgVersion = [ | ||
| "13.21", | ||
| "14.18", | ||
| "15.13", | ||
| "16.9", | ||
| "17.5", | ||
| ] | ||
| } | ||
| name = "postgresql-${index(split(".",cleanVersion(pgVersion)),0)}-standard-bookworm" | ||
| target = "${tgt}" | ||
| args = { | ||
| BASE_IMAGE = "ghcr.io/cloudnative-pg/postgresql:${cleanVersion(pgVersion)}-standard-bookworm", | ||
| EXTENSIONS = "${getExtensionsString(pgVersion, extensions)}", | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Starting at the beginning of the file: | ||
|
|
||
| - The `platforms` variable is `linux/amd64` for all of my images. | ||
| - The `extensions` variable contains some extensions I use regularly. | ||
| - The `dockerfile-inline` part is extended with binaries, some of them are handy | ||
| to have, some needed by extensions or other tools I use e.g. [pgwatch](https://github.com/cybertec-postgresql/pgwatch). | ||
| - With the `sed` command I add needed locales and build them. | ||
| - With the `ADD` commands I extend the image with | ||
| - .psqlrc file, to have a nice psql Command-line even when connecting via | ||
| `kubectl cnpg psql XXX` | ||
| - ldap.conf and the needed certs | ||
|
|
||
| ### Step 2: Build the image | ||
|
|
||
| We can now build the image using the following command: | ||
|
|
||
| ```bash | ||
| 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 | ||
| ``` | ||
|
|
||
| - The `environment` variable is set to `production` for all of my images, | ||
| because I use the same image to stage it through dev/test/prod. | ||
| - The `registry` variable contains the repo upload url, so the images get | ||
| uploaded there instead of the `localhost:5000` registry used in the | ||
| [hcl file](https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg.github.io/refs/heads/main/content/blog/building-images-bake/bake.hcl). | ||
|
|
||
| ### Step 3: Use it | ||
|
|
||
| The only missing step to use the images is to update your | ||
| [Image Catalog / Cluster Image Catalog](https://cloudnative-pg.io/documentation/current/image_catalog/) | ||
| with the newly built images. | ||
| Test them and stage them through your environment. | ||
|
|
||
| ## Conclusion | ||
|
|
||
| Once you prepare the override file to fit to your needs, the only manual steps | ||
| to build new images are | ||
|
|
||
| - udpate the `pgVersion` variable | ||
| - run the `docker buildx bake` command | ||
|
|
||
| I hope this helps streamline your image customization process as much as it | ||
| did mine—feel free to build on it, and share your own improvements too! | ||
| You can find the supportive team in the CloudNativePG channels on the CNCF Slack workspace. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.