# Docker 容器服务重启

* 参数化设定(选择需要重新的服务)

<figure><img src="/files/rLW18lrpHR22qrk4LAkb" alt=""><figcaption></figcaption></figure>

* Pipeline 脚本

<figure><img src="/files/Y222xtFwh12bTEgwtb8d" alt=""><figcaption></figcaption></figure>

```groovy
def IP001 = "1.1.1.1"
def IP002 = "2.2.2.2"
def IP003 = "3.3.3.3"



pipeline {
    agent any
    environment {
        SERVER_IP_STR = ''
    }
    stages {
        stage('Get Choose') {
            steps {
                echo "Restart for environment: $serviceName"
            }
        }
        stage('Init Env') {
            steps {
                echo "Deploying to Restart ${serviceName}"
                script {
                    switch (serviceName) {
                        case 'aaaa':
                            echo "${IP001},${IP002}"
                            SERVER_IP_STR = "${IP001},${IP002}"
                            break
                        case 'bbbb':
                            SERVER_IP_STR = "${IP001},${IP002}"
                            break
                        case 'cccc':
                            SERVER_IP_STR = "${IP001},${IP002}"
                            break
                        case 'dddd':
                            SERVER_IP_STR = "${IP001},${IP002}"
                            break
                        case 'ffff':
                            SERVER_IP_STR = "${IP002},${IP003}"
                            break
                        default:
                            echo "Unknown environment: ${serviceName}"
                            break
                    }
                }
                echo "Environment Variable After: ${SERVER_IP_STR}"
            }
        }
        stage('Ping') {
            steps {
              script {
                echo "Ping address ${SERVER_IP_STR}"
                sh "ansible ${SERVER_IP_STR} -m ping"
              }
            }
        }
        stage('Restart}') {
            steps {
             script {
                echo "Restart Service ${env.SERVER_IP_STR}"
                sh "ansible ${SERVER_IP_STR} -m shell -a 'docker restart ${serviceName}'"
             }
            }
        }
    }
    post {
        failure {
            script {
                // 替换为你的 Telegram bot token 和 chat ID
                def telegramToken = "TG_TOKEN"
                def chatId = "TG_CHAT_ID"
                def message = "Jenkins Build Failed! Job: ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nURL: ${env.BUILD_URL}"

                // 使用 curl 命令发送请求到 Telegram API
                sh """
                    curl -s -X POST https://api.telegram.org/bot${telegramToken}/sendMessage \\
                    -d chat_id=${chatId} \\
                    -d text="${message}"
                """
            }
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/cicd/pipeline/docker-rong-qi-fu-wu-chong-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
