Skip to content

Commit 5e8bb5b

Browse files
smiycjsilvelaFloorD
authored
blog: customizing the docker build bake hcl file (#345)
Signed-off-by: Daniel Chambre <smiyc@pm.me> Signed-off-by: smiyc <36233521+smiyc@users.noreply.github.com> Signed-off-by: Floor Drees <floordrees@gmail.com> Signed-off-by: Jaime Silvela <jaime.silvela@mailfence.com> Co-authored-by: Jaime Silvela <jaime.silvela@mailfence.com> Co-authored-by: Floor Drees <floordrees@gmail.com>
1 parent db7f43a commit 5e8bb5b

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
297 KB
Loading

content/authors/dchambre/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Daniel Chambre
3+
avatar: daniel.jpg
4+
github: smiyc
5+
---
6+
7+
A DBA and Open Source enthusiast.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
platforms = [
2+
"linux/amd64",
3+
]
4+
5+
extensions = [
6+
"dbgsym",
7+
"partman",
8+
"oracle-fdw",
9+
"squeeze",
10+
"show-plans",
11+
"cron",
12+
"tds-fdw",
13+
]
14+
15+
target "myimage" {
16+
dockerfile-inline = <<EOT
17+
ARG BASE_IMAGE="ghcr.io/cloudnative-pg/postgresql:16.9-standard-bookworm"
18+
FROM $BASE_IMAGE AS myimage
19+
ARG EXTENSIONS
20+
USER root
21+
RUN apt-get update && \
22+
apt-get install -y --no-install-recommends $EXTENSIONS \
23+
ldap-utils \
24+
ca-certificates \
25+
openssl \
26+
procps \
27+
postgresql-plpython3-"${getMajor(pgVersion)}" \
28+
python3-psutil \
29+
pgtop \
30+
pg-activity \
31+
nmon \
32+
libsybdb5 \
33+
freetds-dev \
34+
freetds-common && \
35+
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
36+
rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/*
37+
RUN sed -i -e 's/# de_AT.UTF-8 UTF-8/de_AT.UTF-8 UTF-8/' /etc/locale.gen && \
38+
locale-gen
39+
ADD https://your.git.url/postgresql/-/blob/main/.psqlrc?ref_type=heads /var/lib/postgresql/
40+
ADD https://your.git.url/cloudnativepg/-/blob/main/bake/files/etc/ldap/ldap.conf?ref_type=heads /etc/ldap/
41+
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/
42+
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/
43+
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/
44+
RUN update-ca-certificates
45+
USER 26
46+
EOT
47+
matrix = {
48+
tgt = [
49+
"myimage"
50+
]
51+
pgVersion = [
52+
"13.21",
53+
"14.18",
54+
"15.13",
55+
"16.9",
56+
"17.5",
57+
]
58+
}
59+
name = "postgresql-${index(split(".",cleanVersion(pgVersion)),0)}-standard-bookworm"
60+
target = "${tgt}"
61+
args = {
62+
BASE_IMAGE = "ghcr.io/cloudnative-pg/postgresql:${cleanVersion(pgVersion)}-standard-bookworm",
63+
EXTENSIONS = "${getExtensionsString(pgVersion, extensions)}",
64+
}
65+
}
435 KB
Loading
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)