Building Docker images with GitLab CI/CD

By Bart Joris

Subscribe to our blog

At Nubera, we manage all of our code with the versioning tool GIT. All code is stored on a self hosted GIT repository server GitLab. GitLab also offers a built-in CI/CI (Continuous Integration / Continuous Delivery) deployment possibility making it easy for us to test, build and ship our latest code. As an example, we want to show how easy you can create a new Docker image by updating the code.

Our setup: we are editing our code on a laptop that can push code to a GitLab server. On our lab environment we have multiple VM’s that serve as GitLab runners (a GitLab Runner is an isolated VM that picks up jobs from the GitLab server). We will illustrate this with a small demo application that consists of a Docker file. In order to give our Docker image a version tag, we have a separate file defining the version. From that code we create a Docker image, which will be pushed to a private Docker registry.

The Dockerfile:

Gitlab

The version file:

Gitlab

Configuring the GitLab CI/CD

To configure the GitLab CI/CD we need to add a “.gitlab-ci.yml” file, defining all the stages and commands that should be executed. Go here for more information.

Gitlab

Here are the steps used to define the file:

  • Login to the Docker private registry
  • Build the Docker image based on the provide Dockerfile
  • Tag the Docker image of the provided version stored in the version-file
  • Push the Docker image to the private registry

Modifying the Docker file

We can now modify the Docker file and commit the code change through GIT.

Gitlab

We can push the latest code to the GitLab server. This will trigger our CI/CD Pipeline, building a container image based on our latest code.

docker

For now our GitLab Runners are executing the jobs defined in the “.gitlab-ci.yml” file. The output of these jobs are as followed:

Gitlab

docker

After the pipeline has passed, you’ll receive an email.

docker

When all of this is done you can pull the latest Docker image from the Docker private registry:

docker

And we can now run a Docker container based on this image.

Gitlab

This was just a simple application, but of course we can use more complex projects. Right now, the pipeline consists of only one stage, but in production setups you can define extra stages to test the code before building it. Or the code repository can consist of multiple branches (e.g.: development, QA, production, ...).

Need more help building Docker images with GitLab CI/CD?

Feel free to contact us


By Bart Joris on 5 - 05 - 2017

Let’s discuss your situation