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了

相关推荐
csdn_aspnet19 分钟前
在 Docker 上安装和配置 Kafka、选择用于部署 Kafka 的操作系统
docker·kafka
乌托邦的逃亡者5 小时前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
ai小鬼头9 小时前
AIStarter新版重磅来袭!永久订阅限时福利抢先看
人工智能·开源·github
如何原谅奋力过但无声9 小时前
上传GitHub步骤(自用版)
github
OKUNP9 小时前
Docker高级管理--容器通信技术与数据持久化
docker·容器·php
jingshaoqi_ccc11 小时前
GitKraken最后一个免费版本和下载地址
git·github·gitkraken·版本管理工具
乌云暮年11 小时前
Git简单命令
git·gitee·github·batch命令
用户68554496927313 小时前
第一周作业
docker
Albert_Lsk16 小时前
【2025/07/10】GitHub 今日热门项目
人工智能·开源·github·开源协议
星辰云-16 小时前
【超详细】CentOS系统Docker安装与配置一键脚本(附镜像加速配置)
linux·docker·centos