首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >docker从(gha或本地)缓存运行映像

docker从(gha或本地)缓存运行映像
EN

Stack Overflow用户
提问于 2022-08-25 08:19:27
回答 1查看 271关注 0票数 0

我如何执行一个命令(即运行)映像,它只是(本地)构建缓存的一部分(在GHA中),并且没有被推送到注册表中?

这里的完整示例:https://github.com/geoHeil/containerfun

Dockerfile:

代码语言:javascript
复制
FROM ubuntu:latest as builder
RUN echo hello >> asdf.txt

FROM builder as app
RUN cat asdf.txt

ci.yaml GHA工作流:

代码语言:javascript
复制
name: ci
on:
  push:
    branches:
    - main

jobs:
  testing:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up Docker Buildx
      id: buildx
      uses: docker/setup-buildx-action@v2
      
    - name: Login to GitHub Container Registry
      uses: docker/login-action@v1
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: cache base builder
      uses: docker/build-push-action@v3
      with:
        push: True
        tags: ghcr.io/geoheil/containerfun-cache:latest
        target: builder
        cache-from: type=gha
        cache-to: type=gha,mode=max
        
    - name: build app image (not pushed)
      run: docker buildx build --cache-from type=gha --target app --tag ghcr.io/geoheil/containerfun-app:latest .
    

      
    - name: run some command in image
      run: docker run ghcr.io/geoheil/containerfun-app:latest ls /

    - name: one some other command in image
      run: docker run ghcr.io/geoheil/containerfun-app:latest cat /asdf.txt

当使用推送图像时:

代码语言:javascript
复制
docker run ghcr.io/geoheil/containerfun-cache:latest cat /asdf.txt

效果很好。当使用非推送(仅缓存一个)对接程序时,会出现以下故障:

代码语言:javascript
复制
docker run ghcr.io/geoheil/containerfun-app:latest cat /asdf.txt

失败与

代码语言:javascript
复制
Unable to find image 'ghcr.io/geoheil/containerfun-app:latest' locally

为什么会失败?图像不应该至少驻留在本地构建缓存中吗?

编辑

显然:

代码语言:javascript
复制
- name: fooasdf
      #run: docker buildx build --cache-from type=gha --target app --tag ghcr.io/geoheil/containerfun-app:latest --build-arg BUILDKIT_INLINE_CACHE=1 .
      uses: docker/build-push-action@v3
      with:
        push: True
        tags: ghcr.io/geoheil/containerfun-app:latest
        target: app
        cache-from: ghcr.io/geoheil/containerfun-cache:latest

是一种潜在的解决办法,docker run ghcr.io/geoheil/containerfun-app:latest cat /asdf.txt现在工作得很好。然而:

cache

  • requires --使用注册表而不是type=gha将内部构建器映像推送到注册表(我不是want)

  • requires来使用注册表中的映像(拉入运行步骤))。我希望能够简单地运行已经存在的本地映像(这是在

之前的步骤中构建的)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-25 09:14:04

它失败了,因为现在您使用buildx构建映像,并且它只能在buildx上下文中使用。如果您必须在docker上下文中使用该映像,则在使用buildx构建映像时,必须使用参数--load将该映像加载到对接端。请参阅https://docs.docker.com/engine/reference/commandline/buildx_build/#load

因此,将步骤改为如下所示

代码语言:javascript
复制
- name: build app image (not pushed)
  run: docker buildx build --cache-from type=gha --target app --load --tag ghcr.io/geoheil/containerfun-app:latest .

注意:--load参数不支持多重arch构建atm。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73484162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档