Getting Started with GitHub Container Registry instead of Docker Hub

Overview

There is also a story that the free tier of ** Docker Hub ** will be limited, so I checked how to use ** GitHub Container Registry **, so make a note of it. Specifically, I would like to realize the following.

  1. Every time you push to ** GitHub **, build a docker image with ** GitHub Actions ** and push to ** GitHub Container Registry **
  2. At that time, change the tag of the docker image according to the tag pushed to ** GitHub **

environment

--Wsl2 Debian

Advance preparation

** Personal Access Token ** is required to automatically push docker images to GitHub Container Registry [^ 1]. Create from your icon on the top right of GitHub by following the steps Settings> Developer settings> Personal access tokens> Generate new token. The scope is the one in the image below. [^ 1]: In Github Packages, it seems that GITHUB_TOKEN could be used instead.

image.png

Register the obtained token in the GitHub repository that manages Dockerfile etc. according to the procedure of Setting> Secrets> New secret (here, it is assumed that it is saved as CR_PAT).

YAML file creation ①

Create a YAML file to control GitHub Actions. Assuming the following directory structure [^ 2].

.
├── .git
├── .github
│   └── workflows
│       └── build_on_push.yaml
└── Dockerfile

[^ 2]: The GitHub repository actually used is here The contents of build_on_push.yaml are as follows.

name: Publish Docker image
on: push
jobs:
  main:
    name: Push Docker image to Github Container Registry
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.CR_PAT }}
      - name: Push to GitHub Container Registry
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ghcr.io/dr666m1/qiita_starwars:latest

name: is an arbitrary value because it only names workflow and job. In on:, specify the trigger event (push here). Since jobs: on the 3rd line defines a job consisting of 5 steps, each step will be explained briefly below (By the way, runs-on: is the specification of the virtual machine that executes the job. Here, GitHub Host Runner is selected).

Checkout Make the GitHub repository accessible with the action actions / checkout @ v2. Conversely, without this step, you should get an error such as "Dockerfile not found". The documentation is here [https://github.com/marketplace/actions/checkout).

Set up QEMU, Set up Docker Buildx docker / setup-qemu-action @ v1`` docker / setup-buildx-action @ v1` should be recognized as an action to enable the necessary functions.

Login to GitHub Container Registry Login process is performed by the action docker / login-action @ v1. $ {{secrets.CR_PAT}} refers to the Personal Access Token created earlier. It also supports Docker Hub and GitLab, and the Documentation describes how to handle each.

Push to GitHub Container Registry Build and push the docker image with the action docker / build-push-action @ v2. You can also specify tags with tags:, but at this stage, set it to latest.

Operation check

When the file is ready, just git push and the workflow defined in YAML will be executed. You can also check the log as shown in the screen below.

image.png

If there is no problem, you can check the docker image built with Your profile> Packages from your icon on the upper right of GitHub. It seems to be private by default, so change it if you want to make it public. After that, docker run is OK [^ 3] if there is no problem.

docker run -it --rm ghcr.io/dr666m1/qiita_starwars:latest

[^ 3]: As you can see in Dockerfile, in this example the Star Wars screening starts (telnet towel.blinkenlights). .nl) so exit with Ctrl +]> quit

YAML file creation ②

Next, modify the YAML file as follows so that you can specify the tag.

name: Publish Docker image
on: push
jobs:
  main:
    name: Push Docker image to Github Container Registry
    runs-on: ubuntu-latest
    steps:
      - name: Prepare #add to
        id: prep
        run: |
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            TAG=${GITHUB_REF#refs/tags/}
          else
            TAG="latest"
          fi
          echo "::set-output name=tag::${TAG}"
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.CR_PAT }}
      - name: Push to GitHub Container Registry
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ghcr.io/dr666m1/qiita_starwars:${{ steps.prep.outputs.tag }} #Change

The main change is the addition of a step called Prepare. run: specifies the command to be executed by shell (default is bash [^ 4]). Here, the processing is changed by the $ GITHUB_REF environment variable [^ 5], and if the tag name is included, it is assigned, otherwise latest is assigned to TAG. echo" :: set-output name = tag :: $ {TAG} " is workflow command, And here the value of TAG can be referred to in the subsequent step. The part that $ {{steps.prep.outputs.tag}} actually refers to.

[^ 4]: Available shells are Official Documents reference [^ 5]: Available environment variables are Official Documentation See

With this change, the tags of the docker image will also change according to the tags pushed to GitHub. For example, if you execute the following, the tag of the docker image will also be 1.0.

git tag 1.0
git push --tags

image.png

Finally

If you want to move from Docker Hub to GitHub Container Registry, this is generally a problem. GitHub Actions can be used for more things such as automated testing, so I want to study when I have time.

Recommended Posts

Getting Started with GitHub Container Registry instead of Docker Hub
Getting Started with Docker
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
Proceed with Rust official documentation on Docker container (1. Getting started)
[Google Cloud] Getting Started with Docker
Getting Started with Docker with VS Code
Minimal Workflow to push Docker image to Github Container Registry with Github Actions
Docker push to GitHub Container Registry (ghcr.io)
Getting Started with Docker for Mac (Installation)
Getting Started with DBUnit
Getting Started with Ruby
Getting Started with Swift
Getting Started with Doma-Transactions
Push Docker images from GitHub Actions to GitHub Container Registry
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Get started with DynamoDB with docker
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Summarize the main points of getting started with JPA learned with Hibernate
Getting Started with Java_Chapter 5_Practice Exercises 5_4
Getting started with Java lambda expressions
[Linux] Start Apache container with Docker
Build WebRTC Janus with Docker container
Getting Started with Doma-Criteria API Cheat Sheet
Publish Docker Image on GitHub Package Registry
Easy installation of Docker with snap (1 command)
Migrate Dockerhub images to Github Container Registry
Create a container image for arm64 of Kibana and register it in GitHub Container Registry. Start Elastic Stack with Docker Compose on Raspberry Pi 4 (64bit)
The story of updating SonarQube's Docker Container
Getting Started with Parameterization Testing in JUnit
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Introduction of Docker Hub and commands Self-learning ①
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Japanese setting of mysql in Docker container
Getting Started with Creating Resource Bundles with ListResoueceBundle
Test, build, and push your Docker image to GitHub Container Registry using GitHub Actions
How to start with Hyper-V instead of WSL2 on Docker Desktop for Windows
I tried using ECR Public instead of Docker Hub (Docker Hub Download Rate Limit measures)