Skip to content
All Guides
🐳

Docker Build & Push with GitHub Actions

Build multi-stage Docker images and push to GitHub Container Registry with caching.

.github/workflows/ci.yml
name: Docker Build

on:
  push:
    branches: [main]
    tags: ["v*"]
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    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@v5
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ghcr.io/${{ github.repository }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

Setup Steps

  1. 1Uses Docker Buildx for multi-platform and cached builds
  2. 2Authenticates with GitHub Container Registry using GITHUB_TOKEN
  3. 3Builds on every push and PR, but only pushes images on main/tags
  4. 4GitHub Actions cache is used for Docker layer caching (fast rebuilds)
  5. 5Images are tagged with 'latest' — add version tags for releases
Common Pitfalls
  • Missing 'permissions: packages: write' — push will fail without it
  • Large build context — add .dockerignore to exclude node_modules, .git, etc.
  • Multi-stage builds — make sure COPY --from references the correct stage
Pro Tips
  • Use 'cache-from: type=gha' for built-in GitHub Actions Docker caching
  • Add Hadolint step before build to lint your Dockerfile
  • Use multi-stage builds to keep final image size small
Automate This

CI setup done? WarpFix watches your pipeline 24/7 and auto-fixes failures — so you can ship faster.

WarpFix monitors your GitHub repos 24/7. When CI fails, it identifies the error, generates a fix, validates it in a sandbox, and opens a PR — automatically.

Install WarpFix — Free