使用 GitHub Actions 的命名上下文
目录
您可以定义其他构建上下文,
并在 Dockerfile 中使用 或 .什么时候
Dockerfile 定义一个与被覆盖的同名阶段。FROM name
--from=name
这对于 GitHub Actions 重用其他构建或 pin 的结果非常有用 镜像添加到工作流程中的特定标签。
将镜像固定到标签
替换为固定的:alpine:latest
# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello World"
name: ci
on:
push:
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build
uses: docker/build-push-action@v6
with:
build-contexts: |
alpine=docker-image://alpine:3.21
tags: myimage:latest
在后续步骤中使用镜像
默认情况下,Docker Setup Buildx 操作用作构建驱动程序,因此构建的 Docker 镜像不是
自动加载。docker-container
使用命名上下文,您可以重用构建的镜像:
# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello World"
name: ci
on:
push:
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver: docker
- name: Build base image
uses: docker/build-push-action@v6
with:
context: "{{defaultContext}}:base"
load: true
tags: my-base-image:latest
- name: Build
uses: docker/build-push-action@v6
with:
build-contexts: |
alpine=docker-image://my-base-image:latest
tags: myimage:latest
与容器生成器一起使用
如上一节所示,我们没有使用默认的 docker-container
驱动程序进行构建
命名上下文。这是因为此驱动程序无法从 Docker 加载镜像
store 的 SET 文件。要解决此问题,您可以使用本地注册表在工作流程中推送基础镜像:
# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello World"
name: ci
on:
push:
jobs:
docker:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
# network=host driver-opt needed to push to local registry
driver-opts: network=host
- name: Build base image
uses: docker/build-push-action@v6
with:
context: "{{defaultContext}}:base"
tags: localhost:5000/my-base-image:latest
push: true
- name: Build
uses: docker/build-push-action@v6
with:
build-contexts: |
alpine=docker-image://localhost:5000/my-base-image:latest
tags: myimage:latest