为什么退出的docker房客没有被删除?
文件名:dockerHandler.sh
#!/bin/bash set -e to=$1 shift cont=$(docker run -d "$@") code=$(timeout "$to" docker wait "$cont" || true) docker kill $cont &> /dev/null docker rm $cont echo -n 'status: ' if [ -z "$code" ]; then echo timeout else echo exited: $code fi echo output: # pipe to sed simply for pretty nice indentation docker logs $cont | sed 's/^/\t/' docker rm $cont &> /dev/null
但是,无论何时在运行Docker镜像后检查Docker容器状态,它都会提供已退出Docker容器的列表。
命令:docker ps -as
因此,要删除这些退出的容器我手动运行下面的命令
rm $(docker ps -a -f status = exited -q)
您应该将标志--rm
添加到您的--rm
命令中:
从docker工人:
➜ ~ docker run --help | grep rm --rm Automatically remove the container when it exits
删除线
docker kill $ cont&> / dev / null
docker rm $ cont
docker日志$ cont | sed's / ^ / \ t /'
并在Mac中使用gtimeout而不是超时,它工作正常。
在Mac上安装gtimeout:
Installing CoreUtils brew install coreutils In line 8 of DockerTimeout.sh change timeout to gtimeout