Start GitHub Actions self-hosted runner with VirtualBox and Vagrant
Tags: githubactions, github, virtualbox, vagrant
Published: 2021-06-30 03:57:17

![Start GitHub Actions self-hosted runner with VirtualBox and Vagrant](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/064osif2cyhacb6op8qv.jpg)

---

## Overview

- Start GitHub Actions self-hosted runner with VirtualBox and Vagrant on your local machine or a remote machine like Google Compute Engine and EC2.
- Avoid dind (Docker in Docker)
- Accelerate `docker build` (or `docker-compose build`) on the virtual machine with the Docker Layer Caching as the same as your local environment.
- Host OS: Ubuntu 18.04/20.04, macOS, Windows
- Guest OS: Ubuntu 20.04

---

## Install VirtualBox

> [Oracle VM VirtualBox](https://www.virtualbox.org/)

### Ubuntu 18.04/20.04

```sh
sudo apt install -y virtualbox
```

### macOS

```sh
brew cask install virtualbox
```

Allow `Oracle America, Inc.` in the System Preferences.

![Allow Oracle America, Inc.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i226ijrwphz1fhrk5hmx.png)

---

## Install Vagrant

> [hashicorp/vagrant: Vagrant is a tool for building and distributing development environments.](https://github.com/hashicorp/vagrant)

### Ubuntu 18.04/20.04

```sh
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt update && sudo apt install -y vagrant
vagrant autocomplete install
```

### macOS

```sh
brew install vagrant
vagrant autocomplete install
```

---

## Start VM

[![peaceiris/actions-self-hosted-runners - GitHub](https://gh-card.dev/repos/peaceiris/actions-self-hosted-runners.svg?fullname=)](https://github.com/peaceiris/actions-self-hosted-runners)

```sh
git clone https://github.com/peaceiris/actions-self-hosted-runners.git
cd ./actions-self-hosted-runners/images/ubuntu-20.04
git checkout v0.4.0
vim .env
make up
vagrant ssh
cd actions-runner
./config.sh --url https://github.com/[owner]/[repo] --token [token]
nohup ./run.sh &
```

Create `.env` file as follows.

```txt
VB_CPUS = '4'
VB_MEMORY = '8192'
VB_DISK_SIZE = '30GB'
```

- `VB_CPUS`: CPU cores
- `VB_MEMORY`: Memory size
- `VB_DISK_SIZE`: Disk size

![Screen Shot 2021-06-30 at 12.45.44](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/veu9r1clids9bd6o00s6.png)

---

## Use the VM and Accelerate `docker build`

Unlike the GitHub hosted runner, a self-hosted runner is not destroyed for each job, so Docker Layer Caching will work as well as local machine. You don't have to worry about tricky usage of the `actions/cache` and capacity limits. The docker build job became accelerated and we achieved the purpose.

```yaml
name: CI frontend

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  docker:
    runs-on: self-hosted
    timeout-minutes: 3
    env:
      COMPOSE_DOCKER_CLI_BUILD: 1
      DOCKER_BUILDKIT: 1
    steps:
      - uses: actions/checkout@v2.3.4
      - run: hadolint ./Dockerfile
      - run: sudo docker-compose build frontend
```

Slow `docker-compose build` job on the GitHub hosted runner.

![Slow docker-compose build job on the GitHub hosted runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr1lp6kz0nojg2g0u3m2.png)

Accelerated `docker-compose build` job on a self-hosted GitHub Actions runner.

![Accelerated docker-compose build job on a self-hosted GitHub Actions runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi4dv5122rrbzlo3mdz9.png)

Enabled Docker Layer Caching on a GitHub Actions self-hosted runner.

![Enabled Docker Layer Caching on a GitHub Actions self-hosted runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1t6w3walnzjuos3t5to.png)

---

## Repository

{% github peaceiris/actions-self-hosted-runners %}

---

## Conclusion

- We can start a self-hosted GitHub Actions runner on a Ubuntu 20.04 virtual machine with VirtualBox and Vagrant very easily.
- We can accelerate `docker build` (or `docker-compose build`) on the virtual machine with the Docker Layer Caching as the same as your local environment.

Enjoy the comfortable CI/CD! Thank you.