GitHub Actions can push to Docker Hub through an organization OIDC connection instead of keeping a long-lived Docker token in GitHub. Granting id-token: write is not enough: Docker must match only trusted runs and grant access only to the intended image repository. This example publishes a test image from the main branch. It follows official documentation checked on 2026-11-28; it is not a claim of a live test.
Trust Chain and Prerequisites
GitHub issues an identity token; Docker validates it and issues a scoped short-lived access token according to matching rules. The diagram separates permission to exchange a token from permission to access an image repository.
According to Docker and GitHub, the Docker organization needs an eligible Team, Business, Docker Hardened Images or Docker Sponsored Open Source plan, and the operator must be an organization owner or editor. Docker lists 10 connections for Team and the open-source program, 100 for Business, and at most five rulesets per connection; check your own plan before purchasing. Only GitHub is supported; GitHub Enterprise Cloud and Server are not supported for this connection.
id-token: write only lets the job request a GitHub identity token; it does not grant Docker write access. Other steps in the job may request a token too. Enable it only in the publishing job, protect the workflow and main branch, and do not run untrusted PR code in that job. Pin third-party actions to verified full commit SHAs and update them periodically.
Create a Connection and Restrict Publishing
- In Docker Home, select the organization and open Identity & auth → OIDC connections → Create OIDC connection. Create a connection and record its ID.
- Create a ruleset matching the actual GitHub run's
sub. Select the organization's test Docker Hub repository and grant Image Push, which includes pull access. Do not grant organization-wide push just for testing. - Without an environment, the legacy subject for a main-branch run looks like
repo:OWNER/REPO:ref:refs/heads/main. Repositories created after 2026-07-15, or renamed or transferred after that date, may use the immutable default format, for examplerepo:OWNER@OWNER_ID/REPO@REPO_ID:ref:refs/heads/main; older repositories can opt in too. Match the actual format rather than copying placeholders. - When a job specifies a GitHub environment, the default
subusesenvironment:NAMEinstead of a branch ref. Restrict allowed branches and require approvals where appropriate in the GitHub environment; the environment subject alone does not restrict the branch. PRs, tags and custom subjects also change matching.
Docker's ruleset documentation says overlapping matches grant the union of resources and scopes: a narrow pull rule cannot cancel a broader push rule. Docker's interface centers on sub, resources and scopes; do not treat another cloud provider's aud policy fields as mandatory Docker inputs. Audience remains part of recipient validation: use the official action's exchange rather than constructing or logging tokens yourself.
Workflow and Verification
Prepare a GitHub repository containing a Dockerfile and a writable test Docker Hub repository. Set repository Variables DOCKER_ORG, DOCKERHUB_OIDC_CONNECTIONID and DOCKER_IMAGE; the last is the test repository name without the organization prefix. The example uses the fixed SHAs for docker/login-action v4.5.1 and docker/build-push-action v6 listed in GitHub's documentation. OIDC login requires login-action 4.5.0 or later.
name: publish-test-image
on:
push:
branches: [main]
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: Docker Hub login
uses: docker/login-action@abd2ef45e78c5afb21d64d4ca52ee8550d9572c7 # v4.5.1
with:
username: ${{ vars.DOCKER_ORG }}
env:
DOCKERHUB_OIDC_CONNECTIONID: ${{ vars.DOCKERHUB_OIDC_CONNECTIONID }}
- name: Build and push
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6
with:
push: true
tags: ${{ vars.DOCKER_ORG }}/${{ vars.DOCKER_IMAGE }}:${{ github.sha }}
What to verify: the official build action uses Git context by default: it builds from the triggering Git reference, not workspace changes made by earlier steps. Run this against the test repository first. Login and build/push should succeed, and the test repository should contain a tag equal to the commit SHA. Check that the workflow uses neither a password nor another login step before attributing success to OIDC; success alone does not prove that the rule is narrow enough.
Then run the same workflow from a controlled unauthorized branch. The exchange should be denied and no image tag added. If it succeeds unexpectedly, deactivate the connection and inspect all matching rulesets immediately. Keep old credentials until the pilot passes; after checking other workflows and local users do not depend on them, remove unused PATs/OATs from GitHub and revoke them in Docker. If the pilot fails, restore the previous workflow and still-valid credentials; a revoked token must be reissued.
Troubleshooting and Security
- Failure before login: check the publishing job's
id-token: write, variables, organization namespace, action version and connection status. Never print the JWT, request token or Docker credentials. - Exchange denied: compare the connection ID, event, actual subject format and all matching rules. The connection's Failures tab shows qualifying failures and claims, but wrong IDs, deactivated connections and unrelated repositories may not appear. No row does not prove no attempt occurred.
- Login succeeds but push fails: check the image name, existence of the organization repository and whether a matching rule grants Image Push to it. Successful login alone does not establish push permission.
- A short-lived token can still be replayed or abused while valid; OIDC does not prevent a compromised workflow from publishing a malicious image. Protect branches, review workflow changes and secure runners. Self-hosted runners also need isolation and credential cleanup.
Limitations
The example covers automatic exchange for Docker Hub only. dhi.io and registry.scout.docker.com require a separate exchange as described by Docker. Docker also documents DHI and Scout compatibility exceptions: some reads or Scout operations are not fully restricted by image resource scopes. Hub repository rules are not a complete permission model for every Docker product.
FAQ
Is the Docker OIDC connection ID a password?
No. The ID identifies a connection; a job still needs a validated token and a matching rule. Do not rely on obscurity of the ID as the sole security boundary, and never print the identity or exchanged access tokens in workflow logs.
Why does a main-branch rule miss an environment job?
The default subject reflects the environment instead of the branch ref. Match the subject for that environment and restrict deployment branches in GitHub environment protection rules. Check any customized subject format before changing the Docker rule.
Do other PATs still need rotation after migration?
Yes. OIDC only replaces Docker login in workflows actually migrated. PATs/OATs still used by other CI systems, local developers or API clients need their own least-privilege controls and rotation. Inventory dependencies before revoking old credentials.
Related Providers
- GitHub Actions: request an OIDC identity token in a protected publishing job.
- Docker: configure subject, image resources and push scope on the organization connection.
Community & Resources
- Environment Deployment: more deployment and runtime topics.
- GitHub Actions CI/CD guide: workflow and job configuration.
Original source: Docker Blog ↗