Share feedback
Answers are generated based on the documentation.

Docker Build GitHub Actions

GitHub Actions はビルド、テスト、デプロイの連携作業を自動化する、一般的な CI/CD プラットフォームです。 Docker では、開発ワークフロー内で利用可能な GitHub Actions を公式に提供しています。 この Actions は、イメージのビルド、アノテーション、プッシュを行うための、再利用可能で使い方も簡単なコンポーネントです。

以下のような GitHub Actions が利用できます。

Docker のアクションでは簡単なインターフェースを提供します。 それでもビルドパラメーターをカスタマイズできる機能は十分に維持しています。

メモ

For 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 Dockerfile at 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:

  1. Open your repository's Settings.
  2. Under Security, go to Secrets and variables > Actions.
  3. Under Secrets, create a repository secret named DOCKERHUB_TOKEN containing your Docker access token.
  4. Under Variables, create a repository variable named DOCKERHUB_USERNAME containing 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.

利用例

Docker GitHub Actions を使った利用例を確認したい場合は、以下の節を参照してください。