You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI本地Runner拆分Docker镜像构建推送并实现镜像缓存

方案1:利用本地Runner的Docker Daemon共享镜像

因为两个任务都运行在同一个本地GitLab Runner上,且Runner使用shell执行器(或Docker执行器但挂载了宿主机的Docker Socket),build任务构建的镜像会直接保存在Runner的本地Docker镜像仓库中,push任务可直接引用该镜像标签。

修改后的CI配置:

stages:
  - build
  - push

build:
  stage: build
  tags:
    - 你的本地Runner标签  # 确保两个任务绑定同一Runner
  script:
    - docker build -t $IMAGE_TAG .

push:
  stage: push
  tags:
    - 你的本地Runner标签
  script:
    - docker push $IMAGE_TAG

注意:若使用Docker执行器,需在Runner配置中挂载宿主机的/var/run/docker.sock,让任务内的Docker命令直接调用宿主机的Docker Daemon,镜像会存储在宿主机仓库中。

方案2:用Artifacts传递镜像(通用型)

如果担心Runner本地镜像仓库被清理,或需要跨Runner传递镜像,可将镜像导出为tar包,通过GitLab CI的artifacts传递至push任务:

stages:
  - build
  - push

build:
  stage: build
  script:
    - docker build -t $IMAGE_TAG .
    - docker save -o image.tar $IMAGE_TAG
  artifacts:
    paths:
      - image.tar
    expire_in: 1h  # 根据需求设置过期时间

push:
  stage: push
  script:
    - docker load -i image.tar
    - docker push $IMAGE_TAG

该方法不依赖Runner本地镜像仓库,但导出导入tar包会占用磁盘空间与时间,适合镜像体积较小的场景。

内容的提问来源于stack exchange,提问作者Bojan Jovanovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 23:04:59