GitLab : Build Docker Image within CI/CD Pipeline

Posted on Oct 9, 2018

Another brain dump for future reference. This is when setting up gitlab to build and run docker images when the CI/CD pipeline runs.

Install gitlab-runner

On a linux x86-64 download the gitlab-runner package:

sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

Give it permissions:

sudo chmod +x /usr/local/bin/gitlab-runner

Install docker:

curl -sSL https://get.docker.com/ | sh

Create the gitlab-runner user:

sudo useradd –comment ‘GitLab Runner’ –create-home gitlab-runner –shell /bin/bash

Install gitlab-runner:

sudo gitlab-runner install –user=gitlab-runner –working-directory=/home/gitlab-runner

Start the gitlab-runner:

sudo gitlab-runner start

Register the gitlab-runner

Register the gitlab-runner:

sudo gitlab-runner register -n \ –url https://gitlab.com/ \ –registration-token REGISTRATION_TOKEN \ –executor shell \ –description “My Runner”

Add the gitlab-runner user to the docker group:

sudo usermod -aG docker gitlab-runner

Verify the gitlab-runner has docker access:

sudo -u gitlab-runner -H docker info

Creating your .gitlab-ci-yml

You can now test the runner by committing the .gitlab-ci.yml and testing:

before_script:

  • docker info

build_image: script: - docker build -t my-docker-image . - docker run my-docker-image /script/to/run/tests

References: - https://docs.gitlab.com/runner/install/linux-manually.html - https://docs.gitlab.com/ee/ci/docker/using_docker_build.html