【问题标题】:How to run cached Docker image in Github Action?如何在 Github Action 中运行缓存的 Docker 镜像?
【发布时间】:2023-03-02 23:09:01
【问题描述】:

我不知道如何在 Github Actions 中运行缓存的 Docker 映像。
我已经关注了 tutorial 关于发布 Docker 映像的任务,以实现将缓存、构建和推送 Docker 映像到 DockerHub 的任务。
我需要构建、缓存和运行镜像,镜像发布是可选的。
我的目标是加快 CI 工作流程。
这是 Github Actions 工作流程:

name: CI

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Check Out Repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1 

      - name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with: 
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          builder: ${{ steps.buildx.outputs.name }}
          push: true
          tags: ivan123123/c_matrix_library:latest
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

      #- name: Run Docker container
      #  run: ???

      # Upload gcovr code coverage report
      - name: Upload GCC Code Coverage Report
        uses: actions/upload-artifact@v2
        with:
          name: coveragereport
          path: ./builddir/meson-logs/coveragereport/
        
      - name: Upload code coverage reports to codecov.io page
        run: bash <(curl -s https://codecov.io/bash) 

编辑:
我没有找到运行缓存 Docker 映像的解决方案,但是每次我使用 docker/setup-buildx-action@v1 操作运行 CI 工作流时,我都设法构建缓存映像。因为镜像是缓存的,所以我们不需要下载每个 Docker 镜像依赖,从而将时间从原来的 3 分钟节省到只有 40 秒。 以下是 Github Actions 工作流程:

name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Check Out Repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1 

      - name: Cache register
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ hashFiles('**/Dockerfile') }}

      - name: Build Docker image
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          builder: ${{ steps.buildx.outputs.name }}
          load: true
          tags: c_matrix_library:latest
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

      - name: Run Docker container
        run: docker run -v "$(pwd):/app" c_matrix_library:latest

【问题讨论】:

  • 你怎么知道你的action没有使用cashe?有什么错误吗?
  • 动作使用缓存但不知道如何运行缓存的图像? docker run ivan123123/c_matrix_library 会运行缓存的镜像还是从 Dockerhub 拉取镜像?我的目标是加快 CI 工作流程。

标签: docker github-actions image-caching


【解决方案1】:

编辑:

正如 Romain 在 cmets 中提到的那样。初始解决方案将在工作流开始时提取图像,因此不会使用在工作流期间构建的图像。唯一的解决方案似乎是在步骤中自己运行docker run


- name: Run my docker image
  run: >
    docker run -t ivan123123/c_matrix_library:latest
    ...

附带说明。如果您在工作中使用服务,使用此解决方案可能会有点复杂。在这种情况下,你的容器和服务容器之间的网络会很麻烦

原答案:

要运行映像,您可以使用以下命令:


- name: Run my docker image
  uses: docker://ivan123123/c_matrix_library:latest
  with:
    entrypoint: ...
    args: ...

entrypointargs 是可选的。您可以找到更多信息here。但是,一个限制是您可以在 uses 字段中使用任何变量或上下文。您只能硬编码图像的名称和标签。

【讨论】:

  • 这个任务是运行缓存的镜像还是先从 Dockerhub 拉取镜像?
  • "uses" 引用的 docker 镜像是从 DockerHub 中拉取的,这是在开始设置作业时完成的,所以我认为您不能缓存它。如果要使用刚刚构建的 docker 镜像,需要显式调用 docker run。
  • @RomainPrévost 是的,我刚刚意识到。对不起,伊万,但我认为我的回答不是正确的解决方案。就像 Romain 提到的那样,它是在工作流开始时拉取图像。
  • @ITChap 是的,一旦我在 GitHub 集成中对其进行了测试,我现在可以确认它。我将使用我在此期间找到的解决方案来编辑​​我的问题。
  • 我尝试缓存保存构建的 Docker 映像的文件夹,然后像运行任何其他 Docker 映像一样运行它,但由于某种原因我找不到路径。
【解决方案2】:

这可能无法完全回答您的问题,因为我认为没有运行缓存图像的实际方法。

但是你可以使用 Github 的缓存来加速你的构建,我已经发布了一个完整的教程,你可以阅读here

总结一下你可以设置 Docker buildx 然后使用 GH 缓存 使用构建推送操作:

  - name: Set up Docker Buildx
    uses: docker/setup-buildx-action@v1

  - name: Build and push
    uses: docker/build-push-action@v2
    with:
      context: .
      file: ./Dockerfile
      push: true
      tags: ivan123123/c_matrix_library:latest
      cache-from: type=gha
      cache-to: type=gha

编辑

刚刚在 build-push 操作中找到了一个可能对您有用的参考:

https://github.com/docker/build-push-action/blob/master/docs/advanced/share-image-jobs.md

【讨论】:

  • 感谢您的回答,但我想我已经在我的问题的编辑部分发布了一个解决方法,类似于您的“解决方案”。
  • 它很相似......但差异很大。注意缓存参数是完全不同的。您可以转到 build-push 文档并花一些时间阅读那里的说明
猜你喜欢
  • 2023-02-21
  • 2020-08-12
  • 2021-02-13
  • 2022-10-15
  • 2021-12-10
  • 2021-11-22
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多