GitLab allows you to host your own runners to process CI/CD jobs. You can either configure your own local machine or a remote cloud server to run CI/CD jobs but they both come with costs. Running on cloud generates bills while running on your own machine requires to install extra dependencies.

Set up runners in Docker

Docker provides an isolated environment for running containers. You can keep runners on your local machine without any spending, and additionally, it offloads dependency management from you. With Docker containers, you can create/destroy runners without the need to clean up any aftermath.

Quick Start

Before we start, if you have any detailed questions, refer to this official document on GitLab: https://docs.gitlab.com/runner/install/docker/#install-the-docker-image-and-start-the-container.

Let's begin!

  1. Register your runner using volume mounts
docker volume create gitlab-runner-config

docker run -it \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v gitlab-runner-config:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest register
Enter fullscreen mode Exit fullscreen mode

After entering runner's registration details, a config.toml would be saved in the named volume gitlab-runner-config for future use.

2. Publishing ports for website visits

If you CI/CD includes serving a webpage, then don't forget to do port forwarding so that you can access your site via localhost:<PORT> later.

Note that for this to work, your web server in the container should listen to 0.0.0.0:8081 or the container wouldn't be able to detect traffic from your localhost.

docker run -d \
  -p 8081:8081 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v gitlab-runner-config:/etc/gitlab-runner \
  --name gitlab-runner \
  gitlab/gitlab-runner:latest
Enter fullscreen mode Exit fullscreen mode

3. Install required dependencies in your runner

docker exec -it gitlab-runner /bin/bash

# Example
apt-get update
apt-get install -y php-cli screen
Enter fullscreen mode Exit fullscreen mode

You can also use a Dockerfile to make a custom gitlab-runner image if there's too much manual work.

4. Keep it running

The server will keep running unless you explicitly put a stop to it.

Happy Hacking!