Hello!👋 In this article, we can try to register a new gitlab runner in a docker container via:

Before your getting started, you can read the official documentation and don't read this article 😉

So, if you're still here, let's do it together!💪

1️⃣ In you local or virtual machine, you should do those steps:

  1. Pull the GitLab Runner Docker Image: Open your terminal and run the following command to pull the GitLab Runner Docker image: docker pull gitlab/gitlab-runner:latest

  2. Create new volume, it's a good practice to use a Docker volume to persist the config.toml file and ensure it remains available between container restarts. You can create a volume using: docker volume create gitlab-runner-config, where:

    • Names the volume: gitlab-runner-config
  3. Run the GitLab Runner Container:
    Next, run the GitLab Runner container. You can use the following command to start it in detached mode: docker run -d --name yuit-gitlab-runner --restart always -v gitlab-runner-config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest

    This command does the following:

    • Runs the container in detached mode: -d.
    • Names the container yuit-gitlab-runner.
    • Restarts the runner automatically if it stops: --restart always.
    • Maps the configuration directory via: -v gitlab-runner-config:/etc/gitlab-runner: This command mounts a Docker volume named gitlab-runner-config into the filesystem of the container at the specified path (/etc/gitlab-runner).
    • Add docker socket: -v /var/run/docker.sock:/var/run/docker.sock: This command mounts the host's Docker socket file (/var/run/docker.sock) at the same location inside the container.

Explanation of Docker Socket: By mounting the Docker socket, the GitLab Runner can use Docker to run builds as containers. This is necessary for scenarios where your CI/CD pipelines leverage Docker as an executor for running jobs, allowing the GitLab Runner to create and manage Docker containers during the job execution.

2️⃣ Next step is Register runner in gitlab

You have to go to your project in gitlab.com and do those steps:

  • Select Settings ⟶ CI/CD ⟶ Runners

Settings for runners

  • Select New instance runner. new runner in gitlab
  • In the Tags section, in the Tags field, enter the job tags to specify jobs the runner can run, for example, my tag name's is : yuit-runner-tag. If there are no job tags for this runner, select Run untagged jobs

Tags section

  • Runner description: Runner of docker container
  • Select the operating system where GitLab Runner is installed: linux new runner in gitlab token

3️⃣ Move on and you should register runner in local docker container

On your local machine to open terminal and run following command:
docker exec -it yuit-gitlab-runner gitlab-runner register.

In terminal, you need to enter:

  • the GitLab instance URL: https://gitlab.com
  • the registration token: ****
  • the name for the runner: yuit-runner
  • the executor: docker
  • the default Docker image: node:20.16-alpine

docker container

4️⃣ Go to gitlab.com

  • In there we can see that our runner is registered
    registered a new runner

  • After clicking View runners, we can see that our runner is available
    See the new runner