You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/blog/building-images-bake/index.md
+19-22Lines changed: 19 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "Creating a custom container image for CloudNativePG v2.0"
3
-
date: 2025-06-17
3
+
date: 2025-06-23
4
4
draft: false
5
5
image:
6
6
url:
@@ -18,31 +18,31 @@ tags:
18
18
- tutorial
19
19
- bake
20
20
- 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.
22
22
---
23
23
24
24
## 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.
30
30
31
31
We will follow a simple baking recipe to create a custom container image.
32
32
Bake will also allow you to easily build multiple images at the same time.
33
33
34
34
## Ingredients
35
35
36
36
- 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
38
38
39
39
Baking time: 5 minutes.
40
40
41
41
## Instructions
42
42
43
43
### Step 1: Prepare local Bake file
44
44
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):
46
46
47
47
```hcl
48
48
extensions = [
@@ -81,12 +81,11 @@ EOT
81
81
82
82
There are a few things that we should remark here:
83
83
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.
86
85
- 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.
90
89
- The variable `args` lists all the arguments passed to the Dockerfile. We will talk more about this later.
91
90
- The function `getExtensionsString()` is inherited from the Bake file that we reference in the [Ingredients](#ingredients) section
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
102
101
`localhost:5000`, which is the default registry defined for testing environments in the parent Bake file. Let's explain the full command:
103
102
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
+
107
106
In the command above, `-f cwd://bake.hcl` is the local file that we created in Step 1, and
108
107
`-f docker-bake.hcl` is the remote file in the git repo, that we're using to build the image.
109
108
@@ -126,13 +125,11 @@ Using the `--print` flag you can explore the full list of tags created that are
126
125
127
126
### Step 4: Serve the image
128
127
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.
131
129
132
130
## Deep dive into the Bake and Dockerfile
133
131
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.
136
133
137
134
### Bake file
138
135
@@ -143,7 +140,7 @@ It's the base for our custom Bake file.
143
140
The `docker-bake.hcl` file contains a lot of functions that are used to build the images. One of them is the `getExtensionsString()`.
144
141
This function, given the list of extensions we provided, will return a string of the extensions with the correct package name
145
142
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
147
144
`postgresql-16-pgvector,` which is the name of the package for pgvector extensions for PostgreSQL 16 in the Debian
0 commit comments