# Docker builds and caching

Every Kajiya job starts on a fresh VM with an empty Docker cache and an empty workspace. Nothing from the previous job is on the disk, which keeps jobs isolated and reproducible. Speed comes from caches you restore explicitly, as on GitHub-hosted runners.

## Dependency caches with `actions/cache`

`actions/cache` and the `cache:` option of the setup actions work unchanged. The cache is stored in GitHub's cache storage, so restoring it is a download from GitHub to Seoul. Keep entries small and specific:

```yaml
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm          # caches ~/.npm, keyed on package-lock.json
      - run: npm ci
```

```yaml
      - uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: cargo-${{ runner.os }}-${{ hashFiles('Cargo.lock') }}
          restore-keys: cargo-${{ runner.os }}-
```

Cache what you download repeatedly (package manager caches, toolchains), not what is fast to rebuild.

## Docker images: cache layers in a registry or in GitHub

Docker Engine is installed and the `runner` user may use it. Buildx is not preinstalled; `docker/setup-buildx-action` sets it up. Because the local layer cache starts empty on every job, export the cache somewhere and import it on the next build.

### Registry cache

Store the layer cache next to the image, for example in GitHub Container Registry:

```yaml
jobs:
  image:
    runs-on: kajiya-4
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
          cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache
          cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max
```

### GitHub Actions cache backend

Without a registry, Buildx can keep layers in the GitHub Actions cache:

```yaml
      - uses: docker/setup-buildx-action@v3
      - uses: docker/build-push-action@v6
        with:
          context: .
          push: false
          cache-from: type=gha
          cache-to: type=gha,mode=max
```

The Actions cache has a per-repository size limit set by GitHub, and big layer caches evict other entries. Prefer the registry cache for large images.

## Docker Compose and service containers

- `services:` in a job works as on GitHub-hosted runners.
- Docker Compose is not preinstalled. Install the plugin first: `sudo apt-get update && sudo apt-get install -y docker-compose-v2`, then use `docker compose up -d`.

## Docker Hub pull limits

Docker Hub limits anonymous pulls per IP address. Kajiya jobs share the egress addresses of the machines they run on, so log in before pulling from Docker Hub, or pull from another registry:

```yaml
      - uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
```

## Disk space

Each VM has a 30 GB disk, part of which is used by the operating system. Large images and caches count against it. Because the disk is discarded after every job, there is nothing to clean up between jobs.
