如何找到一个Docker报告未运行但存在冲突的容器?
如何找到一个Docker报告未运行但存在冲突的容器?
当我使用指定名称启动一个Docker镜像时,Docker报告该名称已被已经运行的Docker容器占用。我无法通过查看docker ps -a
找到该容器,并且无法通过其ID或名称引用来删除它。我该如何找到冲突的运行容器?我该如何进一步调试这种情况?
myuser@myhostname$ docker --version Docker version 1.11.2, build b9f10c9 myuser@myhostname$ docker run --name=myimage-build ubuntu docker: Error response from daemon: Conflict. The name "/myimage-build" is already in use by container 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e. You have to remove (or rename) that container to be able to reuse that name.. See 'docker run --help'. myuser@myhostname$ docker ps -a | grep 9467 myuser@myhostname$ myuser@myhostname$ docker rm -f 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e Error response from daemon: No such container: 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e myuser@myhostname$ docker rm -f myimage-build Error response from daemon: No such container: myimage-build myuser@myhostname$ myuser@myhostname$ docker run --name=myimage-build ubuntu docker: Error response from daemon: Conflict. The name "/myimage-build" is already in use by container 946747f7608fb17e8f1677152e44a21aeb9f4d3cfda9b30bc7cd7a92411e533e. You have to remove (or rename) that container to be able to reuse that name.. See 'docker run --help'.
admin 更改状态以发布 2023年5月21日
你可以从日志中提取它:
#save log to var res=$(docker-compose --ansi never up -d 2>&1) #check if logs contain error message error_msg="is already in use by container" if [[ "$res" == *$error_msg* ]];then container_id=$(echo $res | sed -r "s/.*$error_msg \"(.*)\".*/\1/g") echo "removing container_id: $container_id" docker rm $container_id fi
另一种方式可以是:如果你不介意删除所有容器以避免冲突,使用:
docker rm $(docker ps -a -q)