简体   繁体   中英

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

I have 2 aws ubuntu instance: 1st-server and 2nd-server.

Below is my jenkins pipeline script which create docker image and runs container on 1st-server and push the image to docker hub repo. 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. 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? 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. This will execute the command on the server and disconnect.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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