Building Docker images with Buildpacks and Gitlab CI

Reading Time: 4 minutes

This blog is second in the Cloud Native Buildpack(CNB) series, If you are new to CNB, refer to previous Introduction blog Introduction to Cloud Native Buildpacks with Kubernetes.

Buildpacks enable us to create a OCI compatible Docker image from application source code, which we can consume very easily using pack cli.

Buildpacks are now part of open source CNCF Buildpacks project.

We can use pack cli to create our Docker images, without even writing a Dockerfile.

For testing we can do it manually, but for actual production deployment, we need CI pipeline for the job.

In this blog you will be creating a CI pipeline using Gitlab, which will use CNB and final image will be pushed to Docker hub.


Creating CI pipeline in Gitlab

Once you have your Gitlab account, sign in and Fork this repository

Once we have forked the repo, next step is to set the environment variables for Docker hub account in repository settings.

Go to Settings and under Settings go to CI/CD

Once we are into CI/CI settings, we can see Variables section to Expand

We will be adding 2 variables here DOCKER_HUB_USER & DOCKER_HUB_PASSWORD, which are your Docker hub credentials, and enable masking, so no credentials are visible during Job execution, click on Save variables.

Gitlab CI/CD variables

In repository there is a file .gitlab-ci.yml.sample which is similar to actual ci file, once we change the name to.gitlab-ci.yml, it will automatically create a pipeline for us

Click on .gitlab-ci.yml.sample file in git repository, click on Edit button in blue, and in edit mode change the file name to .gitlab-ci.yml, click on Commit changes!

Now are pipeline is in place, let’s check our pipeline in CI/CD panel on Gitlab

In pipelines dashboard, we should be able to see our pipeline run status, which was triggered as soon as we committed the gitlab-ci file in our repository.

To check the pipeline run , click either on status or pipeline number, here it is #109397950.

Our pipeline will show the stages and Jobs, lets click on job which build_and_push_image, which will take us to the actual run

Once your Job is successful, you can see newly pushed docker image in your Docker hub account.

Voila!! We have Continuous integration pipeline with Gitlab source repo and Cloud native buildpacks!!

Deep Dive on Gitlab CI configuration file

image: docker:stable 

  DOCKER_DRIVER: overlay2
  REALM: shared
  DOCKER_HOST: tcp://docker:2375/

- name: docker:dind
  entrypoint: ["env", "-u", "DOCKER_HOST"]
  command: [""]

- build

  stage: build
  - wget
  - tar xvf pack-v0.6.0-linux.tgz
  - rm pack-v0.6.0-linux.tgz
  - echo "$DOCKER_HUB_PASSWORD" | docker login --username "$DOCKER_HUB_USER" --password-stdin
  - ./pack build "$DOCKER_HUB_USER"/cnb-goapp --builder --publish

We have used docker:stable as runtime image, so that we can use docker cli to login to Docker hub

Pack cli when publishing the image on docker hub, uses the same credentials as of docker cli.

docker:dind is docker in docker service which is used during runtime.


Ronak Banka

Ronak works as a Senior Solution architect with VMware, based out of Singapore.