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了

相关推荐
掘金安东尼6 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
小Wang15 小时前
npm私有库创建(docker+verdaccio)
前端·docker·npm
逛逛GitHub18 小时前
飞书多维表“独立”了!功能强大的超出想象。
人工智能·github·产品
努力的小雨20 小时前
混元开源之力:spring-ai-hunyuan 项目功能升级与实战体验
后端·github
NocoBase20 小时前
GitHub 上 Star 数量前 10 的开源项目管理工具
开源·github·资讯
绝无仅有20 小时前
面试实战总结:数据结构与算法面试常见问题解析
后端·面试·github
绝无仅有21 小时前
Docker 面试常见问题及解答
后端·面试·github
逛逛GitHub2 天前
斩获 2 万多 Star!国外老哥把全网 n8n 工作流都开源了
github·工作流引擎
杨杨杨大侠2 天前
Atlas Mapper 案例 01:初级开发者 - 电商订单系统开发
java·开源·github
绝无仅有2 天前
后端 Go 经典面试常见问题解析与总结
后端·面试·github