DEV Community

Alfadil mustafa
Alfadil mustafa

Posted on

Build container images in Kubernetes

One of the major steps when you are designing how you gonna ship your app is how you will build it and where.

Some people still use conventional ways to ship their apps where the is some Sysadmin dude who does all the deployment process, Some use automation of some kind, and some use a CI/CD pipeline to achieve this.

Here I'm talking about the last type of teams (The cool one 😎) and precisely the step of building Docker images in Kubernetes environment.

I'm assuming that we have:

  • Dockerfile for the project.
  • Docker hub account.
  • Kubernetes cluster.
  • Kubectl client.

What we will do:

  1. Create a namespace in k8s.
  2. Get authorized to store (push) the resulted image to docker hub (Docker registry).
  3. Deploy a buildkit engine to a Kubernetes cluster.
  4. Build The docker image and push it.

Let's start:
First we should create a namespace
kubectl create namespace images-builder

Then create a Docker hub account.
Go to your terminal and
docker login
then find the docker config file
in linux you will find it in ~/.docker/config.json
it looks like

if you don't find it in that place go search the internet for docker/config.json in the environment you are currently using.

After that we gonna create a secret containing the docker config file we just found using the command
kubectl create secret generic regcred \
--from-file=.dockerconfigjson=/path/to/config.json \
--type=kubernetes.io/dockerconfigjson -n images-builder

Now it's time to deploy the buildkit engine.

save it in a file let's say buildkit.yaml and then
kubectl apply -f buildkit.yaml -n images-builder

Let expose the buildkit engine to use able to use it.

Save it in a file let's say buildkit-service.yaml and then
kubectl apply -f buildkit-service.yaml -n images-builder

We are going to use the engine form our local machine so let's forward the service using
kubectl port-forward service/buildkitd 1234 -n images-builder
And then download the buildkit client from moby/buildkit.
Download the latest version with the version that suits your machine

Extract the files to specific location.

Now we can build the dockerfile using the command.
/path/to/bin/buildctl --addr tcp://127.0.0.1:1234 build --frontend dockerfile.v0 --local context=/path/to/context --local dockerfile=/path/contains/the/Dockerfile

And to build the image and push to the dockerhub
/path/to/bin/buildctl --addr tcp://127.0.0.1:1234 build --frontend dockerfile.v0 --local context=/path/to/context --local dockerfile=/path/contains/the/Dockerfile --output type=image,name=docker.io/username/image-name,push=true

where username is your docker hub username and image-name is the name of the image you would like to use.

Top comments (0)