Help us understand the problem. What is going on with this article?

docker swarmのゾンビコンテナを掃除するスクリプト

More than 1 year has passed since last update.

汚れ仕事ですが、毎回手で掃除するのは面倒なのでスクリプト化しました。コンテナ作成時刻が一番新しいものを残してdocker-containerd-shimプロセスを全部killします。

zc_sweep.sh <service-name>

#!/bin/sh

LIST=`docker ps -f name=$1 --format "{{.CreatedAt}}\t{{.ID}}"`
N=`echo "$LIST" | wc -l`
echo $N containers are found
if [ "$N" = "1" ]; then
  echo ok
  exit
fi
LAST=`echo "$LIST" | sort | head -n-1`
for I in "$LAST"; do
  ID=`echo $I | grep -o '[0-9a-f]*$'`
  PID=`ps aux | grep $ID | grep docker-containerd-shim | awk '{print $2}'`
  echo kill $PID
  kill $PID
  sleep 1
  if ps -p $PID > /dev/null; then
    echo kill -9 $PID
    kill -9 $PID
  fi
done
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away