Skip to content

Commit 5ff3842

Browse files
FloorDsxd
authored andcommitted
Add Jaime's suggestions
Signed-off-by: Floor Drees <[email protected]>
1 parent 7513703 commit 5ff3842

File tree

1 file changed

+19
-22
lines changed
  • content/blog/building-images-bake

1 file changed

+19
-22
lines changed

content/blog/building-images-bake/index.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Creating a custom container image for CloudNativePG v2.0"
3-
date: 2025-06-17
3+
date: 2025-06-23
44
draft: false
55
image:
66
url:
@@ -18,31 +18,31 @@ tags:
1818
- tutorial
1919
- bake
2020
- docker
21-
summary: Creating a container image for CloudNativePG Operator v2.0
21+
summary: Using Docker's Bake to create container images for the CloudNativePG Operator v2.0.
2222
---
2323

2424
## Summary
25-
Almost two years ago, we wrote a [blog post on
26-
building custom container images for CloudNativePG]({{% ref "/blog/creating-container-images/" %}}).
27-
Since then, many things have changed in the world of containers.
28-
One of those things has been the introduction of [Bake](https://docs.docker.com/build/bake/) in Docker, which allows you to build
29-
images using a simple configuration file. Bake is now our recommended way to build images for CloudNativePG.
25+
Almost two years ago, we wrote a [blog post on building custom container images
26+
for CloudNativePG]({{% ref "/blog/creating-container-images/" %}}). Since then, many things have changed in the world of containers.
27+
One of those things has been the introduction of [Bake](https://docs.docker.com/build/bake/) in Docker,
28+
which allows you to build images using a simple configuration file. Bake is now
29+
our recommended way to build images for CloudNativePG.
3030

3131
We will follow a simple baking recipe to create a custom container image.
3232
Bake will also allow you to easily build multiple images at the same time.
3333

3434
## Ingredients
3535

3636
- A Bake file. We will use the one provided in the [CloudNativePG repository](https://github.com/cloudnative-pg/postgres-containers/blob/main/docker-bake.hcl)
37-
- Another Bake file, but this time a local one, this one is to overwrite the previous one.
37+
- Another (local) Bake file, to overwrite the previous one and have a Bake file with your changes applied and build the container images
3838

3939
Baking time: 5 minutes.
4040

4141
## Instructions
4242

4343
### Step 1: Prepare local Bake file
4444

45-
In a local file with name [bake.hcl](bake.hcl), we add the following content, which is a simple Bake file that will build a custom image
45+
To build a custom image we add the following content in a local file with name [bake.hcl](bake.hcl):
4646

4747
```hcl
4848
extensions = [
@@ -81,12 +81,11 @@ EOT
8181

8282
There are a few things that we should remark here:
8383

84-
- The `extensions` variable is a list of extensions that we want to include in the image. In our recipe we are using `pg_vector`,
85-
but you can add any other extension you want.
84+
- The `extensions` variable is a list of extensions that we want to include in the image. In our recipe we are using `pgvector`. But you can add any other extension you want.
8685
- The `dockerfile-inline` variable contains our Dockerfile definition, which cannot be used remotely. We will explain more about this later.
87-
- The `target` and the `tgt` have the same name, you can use whatever you want here as a name
88-
- The `pgVersion` variable is a list that contains basically the MAJOR.MINOR version of PostgreSQL
89-
- The `name` is the name that we will use later to refer to one element of the matrix that we created
86+
- The `target` and the `tgt` have the same name. You can use whatever you want here as a name.
87+
- The `pgVersion` variable is a list that contains basically the MAJOR.MINOR version of PostgreSQL.
88+
- The `name` is the name that we will use later to refer to one element of the matrix that we created.
9089
- The variable `args` lists all the arguments passed to the Dockerfile. We will talk more about this later.
9190
- The function `getExtensionsString()` is inherited from the Bake file that we reference in the [Ingredients](#ingredients) section
9291

@@ -101,9 +100,9 @@ docker buildx bake -f docker-bake.hcl -f cwd://bake.hcl "https://github.com/clou
101100
This will, by default, build the image for the bake matrix we previously created, and will try to push the image to the registry at
102101
`localhost:5000`, which is the default registry defined for testing environments in the parent Bake file. Let's explain the full command:
103102

104-
As is defined in the [Bake documentation about remote definitions](https://docs.docker.com/build/bake/remote-definition/)
105-
you can use a remote Bake definition with all the functions and default targets, and attach another local one that you can use to override
106-
all the default values.
103+
As explained in the [Bake documentation about remote definitions](https://docs.docker.com/build/bake/remote-definition/) you can use a remote Bake definition with all the functions and default targets, and attach another local one to override
104+
all the default values.
105+
107106
In the command above, `-f cwd://bake.hcl` is the local file that we created in Step 1, and
108107
`-f docker-bake.hcl` is the remote file in the git repo, that we're using to build the image.
109108

@@ -126,13 +125,11 @@ Using the `--print` flag you can explore the full list of tags created that are
126125

127126
### Step 4: Serve the image
128127

129-
You can now let your clusters use the image that we've built based on the CloudNativePG operand images provided
130-
by the CloudNativePG project.
128+
You can now let your clusters use the image that we've built based on the CloudNativePG operand images.
131129

132130
## Deep dive into the Bake and Dockerfile
133131

134-
The simplicity of Bake to do even more stuff is amazing, and allows you to create custom images easily.
135-
But, how does this magic happen? Let's take a look at the Bake and the Docker file.
132+
The simplicity of Bake to do even more stuff is amazing, and allows you to create custom images easily.
136133

137134
### Bake file
138135

@@ -143,7 +140,7 @@ It's the base for our custom Bake file.
143140
The `docker-bake.hcl` file contains a lot of functions that are used to build the images. One of them is the `getExtensionsString()`.
144141
This function, given the list of extensions we provided, will return a string of the extensions with the correct package name
145142
for a Debian-based distribution, in our case, Debian Bookworm.
146-
For example, the `pg_vector` extension will be translated into
143+
For example, the `pgvector` extension will be translated into
147144
`postgresql-16-pgvector,` which is the name of the package for pgvector extensions for PostgreSQL 16 in the Debian
148145
Bookworm distribution.
149146

0 commit comments

Comments
 (0)