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



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

完整示例如下:https://github.com/geoHeil/containerfun

Dockerfile:

FROM ubuntu:latest as builder
RUN echo hello >> asdf.txt
FROM builder as app
RUN cat asdf.txt

ci.yaml GHA工作流程:

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

使用推送图像时:

docker run ghcr.io/geoheil/containerfun-cache:latest cat /asdf.txt

它工作得很好。当使用非推送(仅缓存一个(docker失败时:

docker run ghcr.io/geoheil/containerfun-app:latest cat /asdf.txt

故障

Unable to find image 'ghcr.io/geoheil/containerfun-app:latest' locally

为什么会失败?映像不应该至少驻留在本地生成缓存中吗?

编辑

显然:

- 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现在运行良好。但是:

  • 这是使用注册表而不是type=gha作为缓存
  • 需要将内部生成器映像推送到注册表(我不想要(
  • 需要使用注册表中的映像(在运行步骤中提取(。我希望能够简单地运行已经存在的本地映像(它是在之前的步骤中构建的

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

因此,将步骤更改为类似的内容

- 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参数不支持多拱形构建atm

最新更新