Home > Blockchain >  Jenkins CI/CD how to stop running container and start the new pushed one
Jenkins CI/CD how to stop running container and start the new pushed one

Time:01-14

I am building a simple Jenkins pipeline, but when I push the deployment from git is said that the port is already used and I need to stop the running one.

I can't get the container ID

stage("Deploy ") {
            steps{
                script {
                    
                    def dockerCmd = 'sudo docker run -p 8080:8080 -d --init biloocabba/kncare-app:1.0'
                    sshagent(['ec2-server-key']) {
                        sh "ssh -o StrictHostKeyChecking=no [email protected]  ${dockerCmd}"
                    
                    }
                }
            }

        }

CodePudding user response:

You can run script with something like this in Jenkinsfile (here I save the script with name stopByPort.sh)

#!/usr/bin/env bash

for id in $(docker ps -q)
do
    if [[ $(docker port "${id}") == *"${1}"* ]]; then
        echo "stopping container ${id}"
        docker stop "${id}"
    fi
done

Execute:
stopByPort.sh 8080

Link references:
https://stackoverflow.com/a/56953427/14312225

CodePudding user response:

To prevent you need to remember that whole command line before stopping and starting a container there is another way.

Once your CI/CD pushes the updated container to the registry, invoke a single run of Watchtower and it will automatically update the containers.

  •  Tags:  
  • Related