如何获取Docker容器的状态,如果容器未运行则使流水线失败。

huangapple go评论72阅读模式
英文:

How to get Docker container status and fail the pipeline if contianer not running

问题

We have an Azure Pipeline to build and deploy the Docker image.

Once deployed, we need to check the deployed Docker container status and fail the pipeline if the container is not running.

We set up a YAML pipeline to build and deploy the Docker image, which was successfully deployed, and the container is running fine.

But we need to add logic to get the container status. If it's not running, mark the pipeline as failed.

We used the following command to check the status:

sudo docker container ls --filter "name=$(module)" --filter "status=running"

英文:

We have a Azure Pipeline to Build and deploy the Docker image.

Once deploy we need to check the deployed docker container status and fail the pipeline if contianer is not running.

We setup a yaml pipeline to build and deploy the Docker image which successfully deployed and contianer is running fine.

But we need to add a logic to get the contianer status if not running mark the pipeline as failed.

We used the below command to check the status.

sudo docker container ls --filter "name=$(module)" --filter "status=running"

答案1

得分: 0

Step 1: 将下面的代码片段添加到管道中,如果容器未运行,将导致管道失败。

- script: |
   if docker ps | grep busy_wiles; then
      echo '容器正在运行'
    else
      echo '容器未运行'
      exit 1
    fi

注意:在上述代码中,'busy_wiles' 是我的容器名称。

Step 2: 运行管道并查看结果。

• 当容器正在运行时的输出如下。

如何获取Docker容器的状态,如果容器未运行则使流水线失败。

• 当容器未运行时的输出如下。

如何获取Docker容器的状态,如果容器未运行则使流水线失败。

英文:

I have tried to reproduce the same in my lab environment and got positive results after following the below steps.

Step 1: Add the below code snippet to the pipeline which causes the pipeline to fail if the container is not running.

- script: |
   if docker ps | grep busy_wiles; then
      echo 'Container is running'
    else
      echo 'Container is not running'
      exit 1
    fi

*Note: In the above code ‘busy_wiles’ is my container name.

Step 2: Run the pipeline and see the result.

• Below is the output when the container is running.

如何获取Docker容器的状态,如果容器未运行则使流水线失败。

• Below is the output when the container is not running.

如何获取Docker容器的状态,如果容器未运行则使流水线失败。

huangapple
  • 本文由 发表于 2023年2月14日 19:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定