Docker镜像下载-使用github action- 解决无法下载docker镜像的问题

最近不知道怎么jdk的本地镜像被不小心干掉了,然后action全部失败。服务器也不能从远程拉取镜像

由于国内已经无法从docker官方源下载镜像了,但是这个自动化运维的需求还是有的。其实有很多种方法,但是都很麻烦。

这里我写的docker compose执行部署任务的时候会拉取远程的jdk,但是无法连接远程的docker仓库,不过docker的拉取机制是先寻找本地的镜像,如果没有再去拉取

目前有好几种方案下载镜像源

  1. 第一种是挂代理下载这个,这个需要在服务器上配置代理,这个方案很麻烦。所以被我放弃了,
  2. 第二种是使用国内镜像源,不过在前不久,docker国内镜像源也全部失效了。所以这条路也走不通
  3. 第三种是在本地下载镜像源,然后上传到服务器上。不过这种问题是,本机的架构和服务器架构不一致,然后无法运行,像是我上面这图一样。我用的mac是arm64架构,而公司服务器使用的x86_64 x86_64 GNU/Linux

如此看来,还是使用第三种方案好,只需要解决架构不一致导致下载镜像不一样无法运行问题。

下载镜像到远程服务器 github action文件如下

yml 复制代码
name: DEV Pull Docker Image

on:
  workflow_dispatch:
    inputs:
      image_name:
        description: 'The name of the Docker image to pull'
        required: true
      image_tag:
        description: 'The tag of the Docker image to pull'
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      # Pull Docker image from remote repository
      - name: Pull Docker image
        run: |
          docker pull ${{ github.event.inputs.image_name }}:${{ github.event.inputs.image_tag }}

      # Save Docker image to a tar file
      - name: Save Docker image to a tar file
        run: |
          docker save ${{ github.event.inputs.image_name }}:${{ github.event.inputs.image_tag }} -o image.tar

      # Transfer Docker image tar file to the server
      - name: Transfer Docker image to server
        uses: appleboy/scp-action@v0.1.3
        with:
          host: ${{ secrets.DEV_SERVER_HOST }}
          username: ${{ secrets.DEV_SERVER_USER }}
          key: ${{ secrets.DEV_SERVER_SSH_KEY }}
          source: "image.tar"
          target: "/root/code/docker/images/"

      # Use SSH to load the Docker image and run container on the server
      - name: Load Docker image and run container on server
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.DEV_SERVER_HOST }}
          username: ${{ secrets.DEV_SERVER_USER }}
          key: ${{ secrets.DEV_SERVER_SSH_KEY }}
          script: |
            cd /root/code/docker/images/
            docker load -i image.tar

      # Report deployment result
      - name: Notify Deployment Success
        if: success()
        run: echo "Deployment of ${{ github.event.inputs.image_name }}:${{ github.event.inputs.image_tag }} was successful!"

      - name: Notify Deployment Failure
        if: failure()
        run: echo "Deployment of ${{ github.event.inputs.image_name }}:${{ github.event.inputs.image_tag }} failed."

分为五步

Pull Docker image: 从远程仓库拉取 Docker 镜像。

Save Docker image to a tar file: 将 Docker 镜像保存为 tar 文件。

Transfer Docker image to server: 使用 scp-action 将 tar 文件传输到目标服务器。

Load Docker image and run container on server: 使用 SSH 连接到目标服务器,加载 Docker 镜像,并运行容器。

Notify Deployment Success/Failure: 根据部署结果报告成功或失败。

提交之后在action页面输入镜像名称和tag

这样这里输入镜像源的名称和tag,然后点运行,就会自动发布到我的服务器上了。

执行docker images可以发现,已经有了

在服务器里输入docker images已经可以看到镜像已经上传到服务器里面了

重新运行之前失败的action,发现已经找到镜像了,这样就OK了

相关推荐
阿里嘎多学长23 分钟前
2026-05-22 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Dontla1 小时前
Github创建项目(创建仓库、新建项目、新建仓库)步骤
github
放下华子我只抽RuiKe51 小时前
React 从入门到生产(七):性能优化实战
前端·javascript·人工智能·react.js·性能优化·前端框架·github
r-t-H1 小时前
KVM虚拟化与Docker基础实践-第二章
阿里云·docker·容器
晓杰'1 小时前
Balatro后端进阶(2):基于GitHub Actions的CI自动化验证实现
websocket·ci/cd·typescript·node.js·自动化·github·nestjs
析数塔2 小时前
Codegraph 实战:用知识图谱让 AI 编程效率翻倍
人工智能·github
葬送的代码人生3 小时前
别再「Ctrl+C/V」了!Git 开发必备技能,10 分钟告别单机码农
前端·github·代码规范
Lyra_Infra3 小时前
技术排查报告:Kubernetes Ingress 路由异常
docker·架构
码农翻身3 小时前
GitHub,2008年生,2048年卒
github
木雷坞3 小时前
Home Assistant Docker Compose 升级失败排查:镜像、备份和设备映射
服务器·docker·home assisant