简体   繁体   English

如何通过 jenkins 管道在远程服务器上拉取和运行 docker 镜像

[英]How to pull & run docker image on remote server through jenkins pipeline

I have 2 aws ubuntu instance: 1st-server and 2nd-server.我有 2 个 aws ubuntu 实例:第一个服务器和第二个服务器。

Below is my jenkins pipeline script which create docker image and runs container on 1st-server and push the image to docker hub repo.下面是我的 jenkins 管道脚本,它创建 docker 镜像并在第一台服务器上运行容器并将镜像推送到 docker hub 存储库。 That's working fine.这工作正常。

I want to pull image and deploy it on 2nd-server.我想拉取映像并将其部署在第二台服务器上。

When I do ssh for 2nd server through below pipeline script but it logins to 1st-server, even if ssh credential ('my-ssh-key') are of 2nd-server.当我通过下面的管道脚本为第二台服务器执行 ssh 但它登录到第一台服务器时,即使 ssh 凭据('my-ssh-key')是第二台服务器。 I'm confused how it logging to 1st-server and I checked with touch commands so the file is creating on 1st-server.我很困惑它是如何登录到第一台服务器的,我使用触摸命令进行了检查,因此该文件是在第一台服务器上创建的。

pipeline {
  environment {
    registry = "docker-user/docker-repo"
    registryCredential = 'docker-cred'
    dockerImage = ''
  }
  agent any
  stages {
    stage('Cloning Git') {
      steps {
        git url: 'https://github.com/git-user/jenkins-flask-tutorial.git/'
      }
    }
    stage('Building image') {
      steps{
        script {
          sh "sudo docker build -t flask-app-one ."
          sh "sudo docker run -p 5000:5000 --name flask-app-one -d flask-app-one "
          sh "docker tag flask-app-one:latest docker-user/myrepo:flask-app-push-test"
        }
      }
    }
    stage('Push Image') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
          sh "docker push docker-user/docker-repo:flask-app-push-test"
          sshagent(['my-ssh-key']) {
            sh 'ssh -o StrictHostKeyChecking=no ubuntu@2ndserver && cd /home/ubuntu/ && sudo touch test-file && docker pull docker-user/docker-repo:flask-app-push-test'
          }
         } 
        }
      }
    }

My question is, how to login to 2nd server and pull the docker image on 2nd server via through jenkins pipeline script?我的问题是,如何通过 jenkins 管道脚本登录到第二台服务器并在第二台服务器上拉取 docker 镜像? Help me out where I'm doing wrong.帮我找出我做错的地方。

This is more of an alternative than a solution.这与其说是解决方案,不如说是一种替代方案。 You can execute the remote commands as part of ssh.您可以将远程命令作为 ssh 的一部分执行。 This will execute the command on the server and disconnect.这将在服务器上执行命令并断开连接。

ssh name@ip "ls -la /home/ubuntu/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM