Tip: Docker layer caching in CI

1 min read

Tip: Docker layer caching in CI

If your Docker builds in CI take too long, you're probably not leveraging layer caching.

# BAD - invalidates cache on every code change
COPY . .
RUN npm install
 
# GOOD - only invalidates when package.json changes
COPY package*.json ./
RUN npm install
COPY . .

In GitHub Actions, add:

- uses: docker/build-push-action@v5
  with:
    cache-from: type=gha
    cache-to: type=gha,mode=max

I went from 8-minute builds to 2 minutes with this change.