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/[email protected]
        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/[email protected]
        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了

相关推荐
被瞧不起的神2 小时前
Docker 安装教程(CentOS 系统)纯新手可入门
docker·容器·centos
Thinbug3 小时前
群晖Nas - Docker(ContainerManager)上安装GitLab
docker·容器·gitlab
程序员阿超的博客4 小时前
云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?
云原生·容器·kubernetes
m0_635647484 小时前
git管理github上的repository(二)
git·github
程序员阿超的博客5 小时前
云原生核心技术 (2/12): Docker 入门指南——什么是容器?为什么它比虚拟机更香?
docker·云原生·容器
qianmoQ6 小时前
GitHub 趋势日报 (2025年06月08日)
github
Twilight-pending7 小时前
Docker重启流程解析
docker·容器·eureka
测试开发-学习笔记7 小时前
docker的常用命令
docker·容器
GeminiJM8 小时前
Spring boot应用监控集成
运维·docker·容器