Tuesday, June 13, 2017

Stop task and remove unused Dockerimages


Stoptask.sh

#!/bin/bash
aws ecs list-tasks --region us-xxxxx --cluster xxxx --family xxxxxxapp --query taskArns| cut -c46-81 > taskidinfo.out
filename="taskidinfo.out"
IFS=$'\n'
for line in $(cat $filename)
do
aws ecs stop-task --region us-xxxxx --cluster "xxxx" --task $line --reason "stopping new task"
done


remove the container and its related volume

/usr/bin/docker rm -v $(docker ps -aq -f status=exited)

Deploy the new container

mytaskdef=$(aws ecs list-task-definitions --family xxxxxxxxapp --sort desc --max-items 1 --query taskDefinitionArns|cut -c75-76)
echo $mytaskdef > taskd.out
aws ecs run-task --cluster "xxxxxx" --task-definition "xxxxxxxxxapp:`cat taskd.out`"

cleaning the unused docker images

#!/bin/bash
printf '%s'"$((${numberfromcitool}-1))" > del-image.out
dignum=$(docker inspect -f {{.Id}} Registryaddress/docker-xxxxxx:`cat del-image.out`)
echo $dignum > del-image.out
docker rmi -f `cat del-image.out`

Note:dignum means the digest number of the image.

No comments:

Post a Comment