Creating container images with Kaniko inside GitLab CI is fairly easy. Ill show you how.
I tried to build a GitLab CI Pipeline with Docker to build my go app and push it to Docker Hub, that didn’t go well. It always failed and was painfully slow. After hours of research I finally found a way to build and push containers from GitLab CI.
Here Ill show you my typical CI Pipeline for any Golang application which will be deployed in a container image.
You can find the GitLab Repository here.

Docker
One thing I didn’t understand before was what containers really are. I always thought Docker was the only tool to build and run containers, but thats not the case. Docker was one of the first tools to build, push and run containers, but it isn’t the best.
Sure, Docker enabled more developer to use containers and its very convenient to manage everything regarding containers, but nowadays there are a some really good alternatives which make handling containers really easy.
Problems and Alternatives
One problem Docker has is the Daemon. Its big, slow and needs root privileges, which brings performance and security problems with it.
Red Hat created three tools which separated Dockers offer. They are buildah, podman and skopeo, which build, run and push container images really fast and efficiently. They are easy to install on your local Linux machine, but not so easy in your CI Pipeline.
Kaniko
That is the time where Kaniko steps in. Kaniko is a build tool developed by Google, which allows building a container image from a Dockerfile without the Docker Daemon. Kaniko was designed to build container images inside containers or a Kubernetes Cluster.
The main reason I choose Kaniko is that Google provides an container image, where Kaniko is already installed and you can use that image inside your CI Pipeline to build and push your image to a registry.
Now lets look at the Pipeline definition.
The first 3 lines define the stages which the Pipeline will run.
Number 1 is the build stage. Here we build the Go binary, that will show you if you have any syntactical errors inside the code.

The next stage is the testing stage. The pipeline will run all your tests, which you hopefully wrote😁. After these stage you can be sure that your app is correct, but you have to package your app inside a container.

From line 18–25 the pipeline builds the container image and pushes it to the GitLab Container Registry.
It uses the before mentioned image, where Kaniko is installed. Inside the script field you have to define the credentials for the Container Registry, you can skip the part if you arent pushing the image to any Registry, but you have to replace the destination flag with the ` — no-push` flag otherwise Kaniko won’t build the image.

GitLab has some predefined environment variables, which we have to use in the build command. Here you can find all the predefined env vars.
Kaniko needs a context directory where the files reside. We can use the $CI_PROJECT_DIR var, where the project dir is defined. It also needs a Container Registry, where the image is uploaded to. Here we can use the predefined env var from GitLab again. $CI_REGISTRY_IMAGE has the name of the GitLab Container Registry and the container image name and $CI_COMMIT_TAG has the commit tag which we use to version the image.
Thats it. Now you can create your own CI Pipeline for any Go app.
I hope you learned something new and have a great day.
