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了

相关推荐
Garc1 小时前
linux Debian 12 安装 Docker(手动)
linux·docker·debian
苦逼IT运维1 小时前
Kubernetes 双层 Nginx 容器环境下的 CORS 问题及解决方案(极端情况)
运维·nginx·容器·kubernetes·jenkins·运维开发·ci
janthinasnail1 小时前
使用Docker搭建YApi接口管理平台
docker·api接口文档
栗子~~1 小时前
家庭版 windows WSL2 安装Ubuntu 、并在这基础上部署docker、通过本地代理进行联网
windows·ubuntu·docker
熙客2 小时前
Docker核心文件:DockerCompose文件
docker·容器
荣光波比2 小时前
CI/CD(三)—— 【保姆级实操】Jenkins+Docker GitLab+Tomcat 实现微服务CI/CD全流程部署
ci/cd·docker·jenkins
xx.ii2 小时前
k8s:service资源详解
运维·网络·容器·kubernetes
janthinasnail3 小时前
使用Docker搭建DOClever接口管理平台
docker·api接口文档
chinesegf9 小时前
Docker篇4-本地项目app.py与docker加载项目镜像的开发顺序
运维·docker·容器
CyreneSimon9 小时前
Docker 拉取配置教程:解决镜像拉取连接超时问题
运维·docker·容器