Using custom Docker images with GitLab

Aug 21, 2018 by Thibault Debatty | 6602 views

Docker GitLab

https://cylab.be/blog/8/using-custom-docker-images-with-gitlab

One of the interesting features of GitLab is the possibility to automatically run tests when code is pushed to the repository (Continus Integration): https://cylab.be/blog/7/gitlab-quickstart

For most GitLab installations, those tests are executed using Docker containers. But before the actual tests can be executed, additional packages must usually be downloaded and installed, which of course induces significant delays.

Hence it can be an interesting solution to create a custom docker image that will be used for executing the tests. Here is how...

Create a custom image

If it's not done yet, you first have to install Docker.

Your custom Docker image must be described using a Dockerfile. Here is a classical example:

# Initial image
FROM php:7.1

#Install git, the php image doesn't have it
RUN apt-get update -yqq && apt-get install git -yqq

#Install PHP-ZIP extension (used by composer)
RUN apt-get install -yqq libzip-dev
RUN docker-php-ext-install zip

#Install xdebug extension (used for phpunit code coverage)
CMD pecl install xdebug
CMD docker-php-ext-enable xdebug

To build the image, simply type

docker build -t <tag of the image> <directory of Dockerfile>

For example:

docker build -t me/php71:0.0.1 ./

Now that your custom image is ready, you have two possibilities to use it with GitLab: push it on Docker Hub, where it will be available for anybody, or use GitLab registry.

Docker Hub

By pushing your image on docker hub, you make it publicly available to anybody. You first need to create an account.

Once your account is created, you can push your image by typing

docker push <account>/<image>:<tag>

For example:

docker push me/php71:0.0.1

You can now use your image for your tests by modifying your .gitlab-ci.yml:

image: me/php71:0.0.1

GitLab registry

If you don't want your images to be publicly accessible, or don't want to create an account on Docker Hub, you can store your images directly in GitLab. This functionality is called GitLab registry.

This functionality has first to be enabled by the administrator of your GitLab server.

The registry is then accessible in the left menu of your GitLab project. It shows your uploaded images, together with the instructions to push images. These are quite simple:

docker login
docker build
docker push

You can now use your image by indicating the full path in your .gitlab-ci.yml:

image: gitlab.cylab.be:8081/cylab/test

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept