笨办法之docker批量删除image与container

一、前提:

1.要删除image, 必须该image下没有container

2.要删除container,必须使该container处于stop状态

正文:

二、说自己成功的笨办法

1)批量停止container:

sudo docker ps -a | awk '{print $1}'| grep -v CONTAINER | xargs sudo docker stop

2)批量删除container:

sudo docker images | awk '{print $3}' | grep -v IMAGE | xargs sudo docker rmi

3)批量删除image:

sudo docker images | awk '{print $1}' | grep -v REPOSITORY | xargs sudo docker rmi

成功

三、走过的坑:

使用网上说的 sudo docker stop $(docker ps -q) 命令批量停止container——失败

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/containers/json: dial unix /var/run/docker.sock: connect: permission denied
"docker stop" requires at least 1 argument(s).
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

接下来使用网上说的 sudo docker rm -f `docker ps -aq` 命令批量删除container——失败(明显有没停止的,注定后面批量失败)

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

使用网上说的 sudo docker rm $(docker ps --all -q -f status=exited)  命令批量删除container——失败

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

使用网上说的 sudo docker rmi `docker images -q` 命令批量删除images——失败

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/images/json: dial unix /var/run/docker.sock: connect: permission denied
"docker rmi" requires at least 1 argument(s).
See 'docker rmi --help'.

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

使用网上说的 sudo docker image prune -a 成功删掉一部分images

另外,在使用常规的删除命令docker rmi删除image时,遇到提示

Error response from daemon: conflict: unable to delete 536ade400cd9 (cannot be forced) - image has dependent child images
gitlab-runner@yue:~$ sudo docker rmi c8a528e46ad2
Error response from daemon: conflict: unable to delete c8a528e46ad2 (cannot be forced) - image has dependent child images
ent}}' $(docker image ls -q --filter since=536ade400cd9)s}} {{.Id}} {{.Par 
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/images/json?filters=%7B%22since%22%3A%7B%22536ade400cd9%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker image inspect" requires at least 1 argument(s).
See 'docker image inspect --help'.

Usage:  docker image inspect [OPTIONS] IMAGE [IMAGE...]

Display detailed information on one or more images

此时,使用网上说的 列出所有在image之后创建的image的父image 的命令:

docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=xxxxx)后,失败

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.29/images/json?filters=%7B%22since%22%3A%7B%22536ade400cd9%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker image inspect" requires at least 1 argument(s).
See 'docker image inspect --help'.

Usage:  docker image inspect [OPTIONS] IMAGE [IMAGE...]

Display detailed information on one or more images

希望和我一样遇到类似问题的人找到解决办法,也希望能有大神指点一二,谢谢!

你可能感兴趣的:(运维)