Docker BuildKit Laravel CI Speed: Cut Build Time in Half
CI pipelines for Laravel apps often rebuild the whole image on every commit. The result? 20‑30 minute builds that stall releases. Using docker buildkit laravel ci speed tricks you can cache Composer, node_modules, and compiled assets so only changed layers rebuild.
TL;DR / Quick Summary
Enable BuildKit in GitHub Actions (
DOCKER_BUILDKIT=1).Split the Dockerfile into three stages:
base,deps,prod.Cache Composer (
/root/.composer/cache) and npm (/app/node_modules).Add
--cache-fromtodocker buildand push the intermediate image as a cache source.Verify cache hits with
docker buildx imagetools inspect.
Prerequisites
Laravel 11 project with PHP 8.3.
Docker Engine 24+ on the runner.
GitHub repository with Actions enabled.
Basic familiarity with Docker multi‑stage builds.
Rewrite the Dockerfile
Create three stages in Dockerfile at the project root.
# Dockerfile
# ---------- Stage 1: Base image ----------
FROM php:8.3-fpm-alpine AS base
WORKDIR /app
# Install system deps needed for extensions and node
RUN apk add --no-cache \
git \
curl \
libpng-dev \
libzip-dev \
npm
# ---------- Stage 2: Dependency cache ----------
FROM base AS deps
# Copy only lock files to leverage caching
COPY composer.lock composer.json ./
COPY package.json package-lock.json ./
# Install Composer globally (cached layer)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Composer install – caches /root/.composer and vendor
RUN --mount=type=cache,target=/root/.composer/cache \
composer install --no-dev --prefer-dist --no-interaction
# npm install – caches node_modules
RUN --mount=type=cache,target=/app/node_modules \
npm ci --production
# ---------- Stage 3: Production image ----------
FROM base AS prod
COPY --from=deps /app/vendor ./vendor
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Build assets (Vite) – runs only when source files change
RUN npm run build && php artisan view:cache && php artisan route:cache
EXPOSE 9000
CMD ["php-fpm"]Why it works: The deps stage only runs when lock files change. Composer and npm caches live in BuildKit mounts, so subsequent builds reuse them.
Enable BuildKit in GitHub Actions
Add a workflow file at .github/workflows/ci.yml.
name: Laravel CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
env:
DOCKER_BUILDKIT: 1
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build and push image
run: |
docker build \
--cache-from=type=local,src=/tmp/.buildx-cache \
--cache-to=type=local,dest=/tmp/.buildx-cache,mode=max \
-t ghcr.io/${{ github.repository }}:ci-${{ github.sha }} .
docker push ghcr.io/${{ github.repository }}:ci-${{ github.sha }}The --cache-from/--cache-to flags tell BuildKit to reuse layers stored in the runner cache directory.
Verify cache hits
Run the build locally with DOCKER_BUILDKIT=1 docker build . and watch the output. Lines that say Using cache confirm the layer was reused.
$ DOCKER_BUILDKIT=1 docker build .
[+] Building 12.3s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 1.2kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 45B 0.0s
=> [deps 1/3] FROM docker.io/library/php:8.3-fpm-alpine 0.5s
=> CACHED [deps 2/3] RUN curl -sS https://getcomposer.org/installer ... 0.0s
=> CACHED [deps 3/3] RUN npm ci --production 0.0s
=> CACHED [prod 1/2] COPY --from=deps /app/vendor ./vendor 0.0s
=> CACHED [prod 2/2] RUN npm run build && php artisan view:cache ... 0.0sIf you see CACHED instead of RUNNING, the cache is working.
Comparison Table
Metric | Before (legacy builder) | After (BuildKit + multi‑stage) |
|---|---|---|
Image size | ~1.9 GB | ~250 MB |
Full build time | 25 min | 6 min |
Incremental build (no lock change) | 20 min | 1 min |
Common Pitfalls & Troubleshooting
Cache miss on Composer: Ensure
composer.lockis copied beforecomposer install. Changing onlycomposer.jsonforces a full reinstall.npm cache not hit: The
--mount=type=cache,target=/app/node_modulesline must be inside the sameRUNthat runsnpm ci. Splitting into two RUNs breaks the cache.GitHub Actions cache size limit: The default cache limit is 5 GB. If you hit the quota, prune old caches or increase the
max-sizein the cache action.Permission errors in Alpine: The
php-fpmuser may not own/app. AddRUN chown -R www-data:www-data /appin theprodstage if you see permission denied.BuildKit not active: Double‑check the
DOCKER_BUILDKIT=1env var is set for every step that runsdocker build. The runner logs will show#1 [internal] load build definitionwithout the#prefix if BuildKit is off.
FAQ
How do I clear the BuildKit cache in GitHub Actions? Delete the cache entry via the Actions UI or bump the cache key (e.g., add a timestamp) to force a fresh build.
Can I use the same Dockerfile for local development? Yes. Run
docker compose up --buildlocally; BuildKit works on Docker Desktop 24+ out of the box.Do I need a separate Docker registry for the cache image? No. The
--cache-toflag writes to the runner’s filesystem, which theactions/cachestep preserves between jobs.What if my Laravel app uses Horizon workers? Add a separate stage that copies the
supervisorconfig and runsphp artisan horizonin the final image. The caching layers remain unchanged.
For a deeper dive on multi‑stage Laravel builds, see our article on reducing image size with Vite: /articles/laravel-docker-multi-stage-build-vite.
Connect with KoderSolution 🚀
Master modern software engineering, scalable architectures, and advanced backend systems with KoderSolution:
🌐 Official Website: Read more architectural deep-dives and tutorials on kodersolution.com
📺 YouTube Channel: Subscribe to @kodersolution on YouTube for concise, animated system design breakdowns and shorts.
📱 Facebook Page: Follow our official Facebook Page for daily development tips, architecture discussions, and community updates.
💬 What's your setup? How have you tuned Docker BuildKit caches for Laravel CI pipelines, and what unexpected layer invalidations have you run into? Share your thoughts and join the discussion below!