How to containerize a GitHub Actions self-hosted runner

The why


The plan

  1. Create a Dockerfile corresponding to the image of a single runner
  2. Push the image on the Docker Hub
  3. Access the server
  4. Install Docker
  5. Create a compose.yml file
  6. Launch as many runners as you want
  7. Clean up

Create the Dockerfile

The Dockerfile

ARG DEBIAN_FRONTEND=noninteractive
RUN apt update -y && apt upgrade -y && useradd -m dockerRUN apt install -y --no-install-recommends \curl jq build-essential libssl-dev libffi-dev python3 python3-venv python3-dev python3-pip
RUN cd /home/docker && mkdir actions-runner && cd actions-runner \&& curl -O -L${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz \&& tar xzf ./actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz
RUN chown -R docker ~docker && /home/docker/actions-runner/bin/
RUN chmod +x
USER docker

The image start script

REG_TOKEN=$(curl -X POST -H "Authorization: token ${ACCESS_TOKEN}" -H "Accept: application/vnd.github+json"${REPO}/actions/runners/registration-token | jq .token --raw-output)
cd /home/docker/actions-runner
./ --url${REPO} --token ${REG_TOKEN}
cleanup() {echo "Removing runner..."    ./ remove --unattended --token ${REG_TOKEN}}trap 'cleanup; exit 130' INTtrap 'cleanup; exit 143' TERM

Build and push the runner image to Docker Hub

docker build --tag <your-docker-hub-username>/actions-image:latest .
docker push <your-docker-hub-username>/actions-image:latest

Setting up Docker

Preparing the compose.yml file

docker compose up --scale runner=4
Click here to watch super satisfying launch video


Look at them! So eager to please

Cleaning Up

source <user/repo> <github_token>



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store