Skip to main content

Docker イメージの発行

継続的インテグレーション(CI)の一部として、Docker HubやGitHub Packagesといったレジストリに対しDockerイメージを公開できます。

はじめに

このガイドでは、Dockerのビルドを実行し、DockerのイメージをDocker HubあるいはGitHub Packagesに公開するワークフローの作成方法を紹介します。 1つのワークフローで、1つのレジストリあるいは複数のレジストリにイメージを公開できます。

注: 別のサードパーティの Docker レジストリにプッシュする場合は、「GitHub Packages へのイメージの公開」セクションの例が適切なテンプレートとして役立つことがあります。

前提条件

ワークフローの設定オプションと、ワークフローファイルの作成方法についての基本的な知識を持っておくことをおすすめします。 詳しくは、「GitHub Actions について」を参照してください。

以下についての基本的な理解があると役に立つでしょう。

イメージの設定について

このガイドは、GitHubリポジトリ内に保存されたDockerのイメージについての完全な定義を持っていることを前提としています。 たとえば、リポジトリにはイメージを作成するための Docker ビルドを行うのに必要な Dockerfile やその他のファイルが含まれていなければなりません。

定義済みの注釈キーを使って、説明、ライセンス、ソース リポジトリなどのメタデータをコンテナー イメージに追加できます。 詳しくは、「コンテナレジストリの利用」を参照してください。

このガイドでは、Docker build-push-action アクションを使用して Docker イメージをビルドし、1 つまたは複数の Docker レジストリにプッシュします。 詳細については、「build-push-action」を参照してください。

Docker Hubへのイメージの公開

GitHub 上で新しいリリースを作成するたびに、イメージを公開するワークフローを起動できます。 次の例のワークフローは、release イベントが created アクティビティの種類でトリガーされたときに実行されます。 release イベントの詳細については、「ワークフローをトリガーするイベント」をご覧ください。

以下のワークフロー例では、Docker login-action および build-push-action アクションを使用して Docker イメージをビルドし、ビルドが成功した場合は、ビルドされたイメージを Docker Hub にプッシュします。

Docker Hubにプッシュするためには、Docker Hubのアカウントを持っており、Docker Hubのレジストリを作成していなければなりません。 詳細については、Docker ドキュメントの「Docker コンテナー イメージを Docker Hub にプッシュする」を参照してください。

Docker Hub に必要な login-action オプションは次のとおりです。

  • usernamepassword: これは Docker Hub のユーザー名とパスワードです。 ワークフローファイルに公開されないように、Docker Hub のユーザ名とパスワードをシークレットとして保存することをお勧めします。 詳しくは、「GitHub Actions でのシークレットの使用」を参照してください。

Docker Hub に必要な metadata-action オプションは次のとおりです。

  • images: ビルドして Docker Hub にプッシュする Docker イメージの名前空間と名前。

Docker Hub に必要な build-push-action オプションは次のとおりです。

  • tags: DOCKER-HUB-NAMESPACE/DOCKER-HUB-REPOSITORY:VERSION 形式の新しいイメージのタグ。 以下のとおり、単一のタグを設定することも、リストに複数のタグを指定することもできます。
  • push: true に設定した場合は、イメージが正常にビルドされると、レジストリにプッシュされます。
YAML
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。

# GitHub では、コミット SHA にアクションをピン留めすることが推奨されます。
# 新しいバージョンを取得するには、SHA を更新する必要があります。
# タグまたはブランチを参照することもできますが、アクションは警告なしに変更される可能性があります。

name: Publish Docker image

on:
  release:
    types: [published]

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Log in to Docker Hub
        uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
        with:
          images: my-docker-hub-namespace/my-docker-hub-repository

      - name: Build and push Docker image
        uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

上記のワークフローでは、GitHub リポジトリをチェックアウトし、login-action を使用してレジストリにログインしてから、build-push-action アクションを使用して、リポジトリの Dockerfile に基づいて Docker イメージをビルドし、イメージを Docker Hub にプッシュし、タグをイメージに適用します。

GitHub Packagesへのイメージの公開

GitHub 上で新しいリリースを作成するたびに、イメージを公開するワークフローを起動できます。 次の例のワークフローは、release イベントが created アクティビティの種類でトリガーされたときに実行されます。 release イベントの詳細については、「ワークフローをトリガーするイベント」をご覧ください。

以下のワークフロー例では、Docker login-actionmetadata-action、 および build-push-action アクションを使用して Docker イメージをビルドし、ビルドが成功した場合は、ビルド イメージを GitHub Packages にプッシュします。

GitHub Packages に必要な login-action オプションは次のとおりです。

  • registry: ghcr.io に設定する必要があります。
  • username: ${{ github.actor }} コンテキストを使用すると、ワークフロー実行をトリガーしたユーザーのユーザー名を自動的に使用できます。 詳しくは、「コンテキスト」を参照してください。
  • password: 自動的に生成された GITHUB_TOKEN シークレットをパスワードに使用できます。 詳しくは、「自動トークン認証」を参照してください。

GitHub Packages に必要な metadata-action オプションは次のとおりです。

  • images: ビルドする Docker イメージの名前空間と名前。

GitHub Packages に必要な build-push-action オプションは次のとおりです。

  • context: ビルドのコンテキストを、指定されたパス内にあるファイルのセットとして定義します。
  • push: true に設定した場合は、イメージが正常にビルドされると、レジストリにプッシュされます。
  • tagslabels: これらは metadata-action からの出力によって設定されます。

注:

  • このワークフローでは、GitHub によって認定されていないアクションが使われます。 それらはサード パーティによって提供され、個別のサービス使用条件、プライバシー ポリシー、およびサポート ドキュメントが適用されます。
  • GitHub では、コミット SHA にアクションをピン留めすることをお勧めします。 新しいバージョンを取得するには、SHA を更新する必要があります。 タグまたはブランチを参照することもできますが、アクションは警告なしに変更される可能性があります。
YAML
name: Create and publish a Docker image
on:
  push:
    branches: ['release']

Configures this workflow to run every time a change is pushed to the branch called release.

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest

There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.

    permissions:
      contents: read
      packages: write

Sets the permissions granted to the GITHUB_TOKEN for the actions in this job.

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Log in to the Container registry
        uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

Uses the docker/login-action action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

This step uses docker/metadata-action to extract tags and labels that will be applied to the specified image. The id "meta" allows the output of this step to be referenced in a subsequent step. The images value provides the base name for the tags and labels.

      - name: Build and push Docker image
        uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

This step uses the docker/build-push-action action to build the image, based on your repository's Dockerfile. If the build succeeds, it pushes the image to GitHub Packages. It uses the context parameter to define the build's context as the set of files located in the specified path. For more information, see "Usage" in the README of the docker/build-push-action repository. It uses the tags and labels parameters to tag and label the image with the output from the "meta" step.

#
name: Create and publish a Docker image

# Configures this workflow to run every time a change is pushed to the branch called `release`.
on:
  push:
    branches: ['release']

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
    permissions:
      contents: read
      packages: write
      # 
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
      - name: Log in to the Container registry
        uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
      # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
      # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
      # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
      - name: Build and push Docker image
        uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

上記のワークフローは、"リリース" ブランチへのプッシュによってトリガーされます。 GitHub リポジトリをチェックアウトし、login-action を使用して Container registry にログインします。 その後、Docker イメージのラベルとタグを抽出します。 最後に、build-push-action アクションを使用してイメージをビルドし、Container registry に公開します。

Docker HubとGitHub Packagesへのイメージの公開

単一のワークフローで、各レジストリに対して login-action および build-push-action アクションを使用して、Docker イメージを複数のレジストリに公開できます。

次のワークフロー例では、前のセクション (「Docker Hub へのイメージの公開」と「GitHub Packages へのイメージの公開」) の手順を使用して、両方のレジストリにプッシュする単一のワークフローを作成します。

YAML
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。

# GitHub では、コミット SHA にアクションをピン留めすることが推奨されます。
# 新しいバージョンを取得するには、SHA を更新する必要があります。
# タグまたはブランチを参照することもできますが、アクションは警告なしに変更される可能性があります。

name: Publish Docker image

on:
  release:
    types: [published]

jobs:
  push_to_registries:
    name: Push Docker image to multiple registries
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Log in to Docker Hub
        uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Log in to the Container registry
        uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
        with:
          images: |
            my-docker-hub-namespace/my-docker-hub-repository
            ghcr.io/${{ github.repository }}

      - name: Build and push Docker images
        uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

上記のワークフローでは、GitHub リポジトリをチェックアウトし、login-action を 2 回使用して両方のレジストリにログインし、metadata-action アクションでタグとラベルを生成します。 その後、build-push-action アクションによって Docker イメージがビルドされ、Docker Hub および Container registry にプッシュされます。