how to make jenkins pipeline to run docker commands on remote server

Viewed 2002

I have 2 ec2 instances 1st one: that hosts jenkins and docker, which builds an image from a jar file then pushes it to docker hub repo

2nd one: which will be my application server, which will contain docker that will pull this image from docker hub and run the container on the docker engine

Ive done the 1st part which I create the image and push it to docker hub

my question is how to execute a docker commands from the jenkins pipeline to the my application server to pull the image from docker hub and run it

2 Answers

I found a solution for my issue

i used SSH Steps plugin plugin on jenkins

and its written in the pipeline as follows:

    stage('run commands on remote server') {
        steps { 
            script {
                def remote = [:]
                remote.user = 'ec2-user'
                remote.host = 'public or private ip of the remote server'
                remote.name = 'ec2-user'
                remote.identityFile = 'path for your ec2 private key'
                remote.allowAnyHosts = 'true'
                sshCommand remote: remote, command: 'your command 1 here'
                sshCommand remote: remote, command: 'your command 2 here'
            }
        } 
    }

You can use the Jenkins SSH plugin (https://plugins.jenkins.io/ssh/) to and add a build step to Execute shell script on remote host using ssh enter image description here

Or run ssh user@host "docker pull <image> && docker run <image>"

Related