@@ -20,23 +20,26 @@ summary: Creating a container image for CloudNativePG Operator v2.0
2020---
2121
2222## Summary
23- In an almost [ two years old blog post] ( https://cloudnative-pg.io/ blog/creating-container-images/) , we explained how
24- to build a custom container image for CloudNativePG. After two years, many things have changed in the containers world,
25- one of those things was the introduction of [ Bake] ( https://www.docker.com/blog/bake/ ) in Docker, which allows you to build
26- images using a simple configuration file, and it is now our recommended way to build images for CloudNativePG.
23+ In an almost [ two years old blog post] ({{% ref "/ blog/creating-container-images/" %}} ), we explained how
24+ to build a custom container image for CloudNativePG. After two years, many things have changed in the world of containers.
25+ One of those things was the introduction of [ Bake] ( https://www.docker.com/blog/bake/ ) in Docker, which allows you to build
26+ images using a simple configuration file. Bake is now our recommended way to build images for CloudNativePG.
2727
2828We will follow a simple cooking recipe to create a custom container image or a set of container images, since Bake
29- allows you to build multiple images at once in a pretty simple way.
29+ allows you to build multiple images at once in a simple way.
3030
3131## Ingredients
32- - 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 )
3332
34- Cooking time: 5 minutes
33+ - 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 )
34+
35+ Cooking time: 5 minutes.
3536
3637## Instructions
3738
3839### Step 1: Prepare local bake file
40+
3941In a local file with name ` bake.hcl ` , we add the following content, which is a simple bake file that will build a custom image
42+
4043``` hcl
4144extensions = [
4245 "pgvector",
7073 EXTENSIONS = "${getExtensionsString(pgVersion, extensions)}",
7174 }
7275}
73-
7476```
75- There's a couple of things that we can remark here:
76- * The ` extensions ` variable is a list of extensions that we want to include in the image, in this case we are using ` pg_vector ` ,
77+
78+ There are a few things that we should remark here:
79+
80+ - The ` extensions ` variable is a list of extensions that we want to include in the image. In our recipe we are using ` pg_vector ` ,
7781 but you can add any other extension that you want to include in the image.
78- * The ` dockerfile-inline ` variable contains our Dockerfile definition, which cannot be used remotely, will explain more about this later.
79- * The ` target ` and the ` tgt ` have the same name, you can use what ever you want here as a name
80- * The ` pgVersion ` is a variable list that contains basically, the MAJOR.MINOR version of PostgreSQL
81- * The ` name ` is the name that we will use later to refer to one element of the matrix that we created
82- * The ` args ` is all the arguments passed to the Dockerfile, will talk more about this later.
83- * The function ` getExtensionsString() ` is one that is inherited from a bake file that we reference in the [ Ingredients] ( #ingredients ) section
82+ - The ` dockerfile-inline ` variable contains our Dockerfile definition, which cannot be used remotely. We will explain more about this later.
83+ - The ` target ` and the ` tgt ` have the same name, you can use what ever you want here as a name
84+ - The ` pgVersion ` is a variable list that contains basically, the MAJOR.MINOR version of PostgreSQL
85+ - The ` name ` is the name that we will use later to refer to one element of the matrix that we created
86+ - The ` args ` lists all the arguments passed to the Dockerfile. We will talk more about this later.
87+ - The function ` getExtensionsString() ` is inherited from the bake file that we reference in the [ Ingredients] ( #ingredients ) section
8488
8589### Step 2: Build the image
8690
8791We can now build the image using the following command:
92+
8893``` bash
8994docker buildx bake -f docker-bake.hcl -f cwd://bake.hcl " https://github.com/cloudnative-pg/postgres-containers.git" myimage
9095```
91- This will, by default, build the image for the bake matrix we previously created and will try to push the image to the registry
92- ` localhost:5000 ` , which is the default registry defined for testing environments in the parent bake file, so let 's explain the full command first.
96+ 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
97+ ` localhost:5000 ` , which is the default registry defined for testing environments in the parent bake file. Let 's explain the full command first.
9398
94- As is defined in the [ Bake documentation about remote definition] ( https://docs.docker.com/build/bake/remote-definition/ )
95- 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
96- all the default values, in this case the ` -f cwd://bake.hcl ` is the local file that we created in the previous step, and
97- the ` -f docker-bake.hcl ` is the remote file that we're using to build the image, this is in the git repo.
99+ As is defined in the [ Bake documentation about remote definitions] ( https://docs.docker.com/build/bake/remote-definition/ )
100+ 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
101+ all the default values.
102+ In the command above, ` -f cwd://bake.hcl ` is the local file that we created in the previous step, and
103+ ` -f docker-bake.hcl ` is the remote file that we're using to build the image, which is in the git repo.
104+
105+ You can explore more about all the content generated and used inside the bake file by appending the ` --print ` flag, as in the following command:
98106
99- You can explore more about all the content generated and used inside the bake file with the following command:
100107``` bash
101108docker buildx bake -f docker-bake.hcl -f cwd://bake.hcl " https://github.com/cloudnative-pg/postgres-containers.git" myimage --print
102109```
103110
104111### Step 3: Push the image to a registry
105112
106113Now you just need to push the image to a registry, you can do this by using the following command:
114+
107115``` bash
108116registry=your/registry:5000 docker buildx bake -f docker-bake.hcl -f cwd://bake.hcl " https://github.com/cloudnative-pg/postgres-containers.git" myimage --push
109117```
110- The previous command will push the images in the following format ` your/registry:5000/postgresql-testing:17-standard-bookworm ` ,
111- using the ` --print ` you can explore the full list of tags created that are in the parent bake file.
118+
119+ The previous command will push the images in the following format: ` your/registry:5000/postgresql-testing:17-standard-bookworm ` .
120+ Using the ` --print ` flag you can explore the full list of tags created that are in the parent bake file.
112121
113122### Step 4: Serve the image
114123
115- You can now let your clusters to use the image that it's built and based on the CloudNativePG operand images provided
124+ You can now let your clusters use the image that we've built based on the CloudNativePG operand images provided
116125by the CloudNativePG project.
117126
118127## Deep dive into the Bake and Dockerfile
119128
120- The simplicity of Bake to bring more stuff is amazing and allows you to create a custom image in a very simple way.
121- But, how is this magic happens ? Let's take a look at the Bake and the Docker file.
129+ The simplicity of Bake to do even more stuff is amazing, and allows you to create custom images in a very simple way.
130+ But, how does this magic happen ? Let's take a look at the Bake and the Docker file.
122131
123132### Bake file
124133
125134This is the base of our magic, but that magic starts in our postgres-containers repository, where we have a ` docker-bake.hcl ` file
126- that it's being used to build the images provided by the CloudNativePG project, and it's the base for our custom Bake file.
135+ that is being used to build the images provided by the CloudNativePG project, and it's the base for our custom Bake file.
127136
128-
129- The ` docker-bake.hcl ` file contains a lot of functions that are used to build the images, and one of them is the ` getExtensionsString() ` ,
130- this one, using the list of extensions we provided, will return a string of the extensions with the correct package name
131- for a Debian-based distribution, in our case, Debian Bookworm, so the ` pg_vector ` extension will be translated into
132- ` postgresql-16-pgvector ` which will be the name of the package of pgvector extensions for PostgreSQL 16 in the Debian
137+ The ` docker-bake.hcl ` file contains a lot of functions that are used to build the images, and one of them is the ` getExtensionsString() ` .
138+ This function, using the list of extensions we provided, will return a string of the extensions with the correct package name
139+ for a Debian-based distribution, in our case, Debian Bookworm.
140+ So the ` pg_vector ` extension will be translated into
141+ ` postgresql-16-pgvector ` which is the name of the package for pgvector extensions for PostgreSQL 16 in the Debian
133142Bookworm distribution.
134143
135- Even the variable that are passed in the ` args ` variable to the Dockerfile are processed by the Bake file, in this case,
144+ Even the variables that are passed in the ` args ` variable to the Dockerfile are processed by the Bake file, in this case,
136145the variables we add, are being added and the ones we overwrite are being overwritten, so we can use the same variables
137146and content used in the parent Bake file.
138147
@@ -145,10 +154,12 @@ on the CloudNativePG images, and we can add the extensions we want to use in our
145154## There's more!
146155
147156You may want to avoid building arm64 image by adding the following:
157+
148158``` hcl
149159platforms = ["linux/amd64"]
150160```
151- This will overwrite the platforms variable and so, will build only for one platform
152161
153- Also, if you want to build everything into your own repository and manage the same tags, it's possible, in the future
154- we may write another post explaining this
162+ This will overwrite the platforms variable and so, will build only for one platform.
163+
164+ Also, if you want to build everything into your own repository and manage the same tags, it's possible. In the future
165+ we may write another post explaining this.
0 commit comments