Skip to content
Sniptide

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

Files

.github/workflows/docker.ymlRaw
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

Save with your agent

Run this command to add Sniptide to Claude Code in the current project. Open /mcp in Claude Code and sign in to Sniptide. Once the server shows as connected, use the prompt below to save a private copy in your library.

Terminal
claude mcp add --scope local --transport http sniptide https://app.sniptide.com/api/mcp
Prompt
Read https://sniptide.com/templates/github-actions-docker.json and use create_snippet to save its files as a private Sniptide snippet titled "GitHub Actions Docker build and push". Keep the file names and contents from the JSON.

Run it

The workflow needs a Dockerfile at the repository root, such as the Python or Next.js 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:

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

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

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

.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.

All templates
  • Project setupGitHub Actions Node CI

    A GitHub Actions workflow for Node.js that lints, type checks, tests and builds on pushes to main and pull requests, with pnpm, matrix and Postgres variants.

  • Docker and deployPython Dockerfile with uv

    A multi-stage Python Dockerfile that installs dependencies with uv from a lockfile, caches them between builds and runs the app as a non-root user.

  • Docker and deployNext.js Dockerfile

    A multi-stage Next.js Dockerfile that builds the standalone output into a small image running as a non-root user, with .dockerignore and variants.

Pick where your library lives.

Use sniptide.com

Save your first pattern in the app, then connect Claude Code, Cursor or Codex with one sign-in in the browser.

Run it on your own server

One image and a database give you the app, the API and the MCP endpoint. Sniptide is open source under AGPL-3.0.

$docker compose up -d