# GitHub Actions Docker build and push

> This workflow builds your Dockerfile on every pull request and pushes the image to GitHub Container Registry when you push to main or a version tag, tagged by branch, version and commit. Save it as .github/workflows/docker.yml in a repository with a Dockerfile at the root and push to main.

Updated November 30, 2026. Canonical: https://sniptide.com/templates/github-actions-docker
Open it in the Sniptide editor: https://app.sniptide.com/new?template=github-actions-docker
Files as JSON: https://sniptide.com/templates/github-actions-docker.json

## Files

### .github/workflows/docker.yml

```yaml
name: Docker

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

concurrency:
  group: docker-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  image:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false

      - uses: docker/setup-buildx-action@v4

      # Pull requests only build the image. Pushes to main and version tags also publish it.
      - if: github.event_name != 'pull_request'
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - id: meta
        uses: docker/metadata-action@v6
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha

      - uses: docker/build-push-action@v7
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
```

## Run it [#run-it]

The workflow needs a `Dockerfile` at the repository root, such as the [Python](/templates/python-dockerfile) or [Next.js](/templates/nextjs-dockerfile) one. It signs in to GitHub Container Registry with the built-in `GITHUB_TOKEN`, so there is no secret to create.

Push the workflow to `main` and open **Actions → Docker** to watch the run. When it finishes, the image appears under **Packages** on the repository page. Pull it with:

```bash title="Terminal"
docker pull ghcr.io/OWNER/REPO:main
```

Replace `OWNER/REPO` with your repository in lowercase. A new package is private by default. To make it public, open the package, then **Package settings → Change visibility**.

## What each step does [#what-each-step-does]

| Step | What it does | When to change it |
| --- | --- | --- |
| `on` | Runs on pull requests, pushes to `main`, tags like `v1.4.0` and manual runs. | Remove `tags` if you don't release by tag. |
| `permissions` | `packages: write` lets this job push to the registry. Everything else stays read-only. | Rarely. |
| `setup-buildx-action` | Sets up BuildKit, which the cache and multi-platform builds need. | Rarely. |
| `login-action` | Signs in to `ghcr.io`, skipped on pull requests because they never push. | Change `registry` and use secrets for Docker Hub or another registry. |
| `metadata-action` | Works out the tags and OCI labels from the event. | Change the `tags` rules to match how you deploy. |
| `build-push-action` | Builds the image and pushes it when the event isn't a pull request. | Set `context` or `file` when the Dockerfile isn't at the root. |
| `cache-from` and `cache-to` | Store build layers in the GitHub Actions cache, so unchanged layers aren't rebuilt. | Rarely. |

The tags each event produces:

| Event | Tags |
| --- | --- |
| Push to `main` | `main`, `sha-<short commit>` |
| Tag `v1.4.0` | `1.4.0`, `1.4`, `sha-<short commit>` |
| Pull request 42 | `pr-42`, built but not pushed |

Deploy from the `sha-` or version tag rather than `main`, so a rollback points at an exact image.

## Variant: build for ARM and x86 [#variant-build-for-arm-and-x86]

Add QEMU and list the platforms. Emulated ARM builds are slower, so expect a longer run:

```yaml title=".github/workflows/docker.yml"
      - uses: docker/setup-qemu-action@v4

      - uses: docker/setup-buildx-action@v4

      # ...login and metadata as above...

      - uses: docker/build-push-action@v7
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
```

Keep action versions current with the `github-actions` ecosystem in [dependabot.yml](/templates/dependabot-yml).
