How to Use Cosign to Sign and Verify Container Images in CI/CD with Sigstore

Docker tutorial - IT technology blog
Docker tutorial - IT technology blog

The Real-World Risk of Pushing Container Images to Production

If you have worked with Docker long enough, you will eventually take a hard look at your team’s deployment pipeline and feel a chill. CI builds the image and pushes it to Docker Hub or a private registry with a tag like v1.2.0 or latest. Then the production servers simply pull it down and run it.

Everything runs smoothly until the day something goes wrong. A teammate accidentally overwrites an existing tag, or a registry token gets leaked. All an attacker needs to do is push a malicious image using that exact same tag.

When Docker or Kubernetes pulls an image, it only checks whether the downloaded file suffered any network corruption. It has no idea who built the image, nor whether the image was tampered with after leaving CI.

When you are just starting out with Docker, getting a deployment to run is already a win. But once your system grows to 20–30 services, a single swapped image in the middle of the pipeline is enough to bring down the entire production cluster.

Why Tags and SHA256 Digests Are Still Not Enough

Many engineers assume that ditching the latest tag and pinning an immutable @sha256:... digest guarantees complete safety. In reality, that is not quite the case:

  • Tags are mutable: Anyone with push permissions on the registry can push a new build that overwrites an existing v1.2.0 tag. Your deployment system will not raise any clear warning.
  • Digests prove integrity, not provenance: A SHA256 hash confirms that the downloaded image matches the manifest on the registry. However, if an attacker swaps the manifest and layers at push time, the new digest will still check out as valid.
  • Traditional key management is cumbersome: Using GPG or Docker Content Trust (Notary v1) requires generating and securely storing private keys, then rotating them every 6–12 months. If someone leaves the team or a key leaks in a CI environment variable, the whole team has to start over from scratch.

This exact operational overhead is why many teams skip image signing altogether and accept the risk.

Two Ways to Sign and Verify Container Images with Cosign

To close this gap, the Cloud Native community developed the Sigstore standard, with Cosign as its flagship tool. Depending on your existing infrastructure, there are two main ways to adopt it.

Method 1: Signing with a Static Key Pair

This approach works well for self-hosted private registries or on-prem CI/CD servers that do not support OIDC yet. You generate a private/public key pair locally, keep the private key secret, and distribute the public key to your deployment servers for verification.

Install Cosign on Linux or macOS:

# Quick install via Homebrew (macOS/Linux)
brew install cosign

# Check the version after installing or downloading the binary from GitHub Releases
cosign version

Next, generate a key pair and sign a test image:

# Generate cosign.key (private) and cosign.pub (public)
cosign generate-key-pair

# Sign the image by digest (recommended over signing by tag)
cosign sign --key cosign.key ghcr.io/your-org/my-app@sha256:abc123...

# Verify the image before running
cosign verify --key cosign.pub ghcr.io/your-org/my-app@sha256:abc123...

Quick tip: Cosign stores signatures as OCI artifacts (with a .sig tag) directly in your existing container registry, so you do not need to spin up a separate database.

Method 2: Sigstore Keyless Signing (No Private Keys to Manage)

If you would rather avoid the risk of managing a cosign.key file, this is Cosign’s standout feature. It leverages the built-in OIDC (OpenID Connect) identity provider in GitHub Actions or GitLab CI.

The entire signing flow takes about 2–3 seconds:

  1. The CI runner requests a short-lived digital certificate (an ephemeral certificate valid for around 10 minutes) from Sigstore’s Fulcio service based on the workflow’s OIDC identity.
  2. Cosign signs the image using that ephemeral key and immediately discards it.
  3. The signing record is written to Rekor, a public, tamper-evident transparency log.

This eliminates the worry of leaking or losing a private key while still proving that the image was built by a workflow from your official repository.

Standardizing Cosign Keyless in CI/CD and Practical Tips

For GitHub Actions or GitLab CI, the best setup is Keyless Signing paired with strict identity verification before deployment.

The following GitHub Actions workflow (for example, at .github/workflows/release.yml) builds, pushes, and signs your image automatically in a single job:

name: Build and Sign Container Image
on:
  push:
    branches: ["main"]

jobs:
  build-and-sign:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id-token: write # Required to fetch an OIDC token for Cosign Keyless

    steps:
      - name: Checkout source
        uses: actions/checkout@v4

      - name: Install Cosign
        uses: sigstore/cosign-installer@v3

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        id: push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:v1.0.0

      - name: Sign image with Cosign (Keyless)
        run: |
          cosign sign --yes ghcr.io/${{ github.repository }}@${{ steps.push.outputs.digest }}

On the deployment server, add a verification command before running docker compose up or kubectl apply:

cosign verify \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com \
  --certificate-identity-regexp "^https://github.com/your-org/your-repo/.github/workflows/release.yml@refs/heads/main$" \
  ghcr.io/your-org/your-repo@sha256:abc123...

5 Battle-Tested Tips to Avoid Pitfalls in Production

  • Always lock down --certificate-identity and --certificate-oidc-issuer: Never run a generic cosign verify. If you do not specify the exact repository and workflow URL, anyone using GitHub Actions could sign a forged image and easily bypass your check.
  • Sign by digest, not by tag: In your pipeline, always pass steps.push.outputs.digest to cosign sign. That binds the signature to the exact binary contents of the image and prevents tag-swapping attacks.
  • Pass the --yes flag in automated environments: Newer versions of Cosign enable keyless signing by default and prompt for confirmation before uploading entries to the public Rekor log. Without --yes, your CI/CD job will hang waiting for input until it times out.
  • Shift heavy verification to the deploy gate instead of running agents on every node: Previously, our team ran scanning and verification agents continuously on every container node, which consumed significant CPU and RAM. On a production cluster running 30+ containers, moving Cosign verification to a single check at the CI/CD gate and admission controller cut resource usage by 40%.
  • Be careful with private repositories: When using Keyless Signing with the public Rekor instance, your image digest, OIDC workflow URL, and signing timestamp are recorded in a public transparency log. If you need to keep internal repository names private, self-host your own Sigstore stack—or fall back to Method 1 backed by a KMS (AWS KMS, GCP KMS) to keep your signing keys secure.

With just about 15 extra lines of YAML in your pipeline and a single verification command before deployment, your Docker workflow becomes dramatically more secure without slowing down your team’s release velocity.

Share: