Docker Build GitHub Actions
GitHub Actions is a popular CI/CD platform for automating your build, test, and deployment pipeline. Docker provides a set of official GitHub Actions for you to use in your workflows. These official actions are reusable, easy-to-use components for building, annotating, and pushing images.
The following GitHub Actions are available:
- Build and push Docker images: build and push Docker images with BuildKit.
- Docker Buildx Bake: enables using high-level builds with Bake.
- Docker Login: sign in to a Docker registry.
- Docker Setup Buildx: creates and boots a BuildKit builder.
- Docker Metadata action: extracts metadata from Git reference and GitHub events to generate tags, labels, and annotations.
- Docker Setup Compose: installs and sets up Compose.
- Docker Setup Docker: installs Docker Engine.
- Docker Setup QEMU: installs QEMU static binaries for multi-platform builds.
- Docker Scout: analyze Docker images for security vulnerabilities.
Using Docker's actions provides an easy-to-use interface, while still allowing flexibility for customizing build parameters.
NoteFor an alternative to configuring individual actions, see Docker GitHub Builder. It provides Docker-maintained reusable workflows for building images and artifacts with BuildKit.
Get started
Build an image from your GitHub repository and push it to Docker Hub with the following workflow.
Prerequisites
- A GitHub repository with your application and a
Dockerfileat its root. - A Docker Hub account and a personal access token with permission to push images.
Configure your GitHub repository
Add your Docker Hub credentials to your GitHub repository:
- Open your repository's Settings.
- Under Security, go to Secrets and variables > Actions.
- Under Secrets, create a repository secret named
DOCKERHUB_TOKENcontaining your Docker access token. - Under Variables, create a repository variable named
DOCKERHUB_USERNAMEcontaining your Docker Hub username.
Set up your GitHub Actions workflow
Create .github/workflows/docker-ci.yml in your repository with the following
contents. Replace my-image with the name of your Docker Hub repository, and
change main if your default branch has a different name.
name: Build and push Docker image
on:
push:
branches:
- main
pull_request:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Extract Docker image metadata
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ vars.DOCKERHUB_USERNAME }}/my-image
- name: Sign in to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build and push Docker image
uses: docker/build-push-action@v7
with:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}Commit the workflow file and push it to your default branch to run it. The workflow also runs on pull requests. Pull-request builds skip signing in to Docker Hub and pushing the image, so they don't require registry secrets, including for pull requests from forks.
The build action uses Git context by default: BuildKit fetches your repository directly at the Git reference for the event that triggered the workflow, so no checkout step is needed. Changes to files made by earlier workflow steps are ignored because the build uses files from Git.
The metadata action generates tags, labels, and
annotations from the GitHub event and passes them to the build step. For
example, a push to main publishes DOCKERHUB_USERNAME/my-image:main.
See
Manage tags and labels
and
Annotations to customize
this metadata.
This workflow builds images but doesn't run application tests. To add tests, see Test before push. For additional build options, see Multi-platform images and SBOM and provenance attestations, which covers default provenance and how to add SBOM attestations when pushing to a registry.
Examples
If you're looking for examples on how to use the Docker GitHub Actions, refer to the following sections: