使用docker remove获得权限拒绝的错误

1 人不认可

我正在学习一个教程,在当前的步骤中,我应该用这个方法删除任何预先存在的docker容器

docker rm -f $(docker ps -aq)

我通常必须使用sudo才能使用docker命令,所以我尝试了一下

sudo docker rm -f $(docker ps -aq)

But I get this

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.32/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers

通常,当我忘记使用sudo时,会出现权限错误,但在这种情况下,我有这个权限。

有没有人知道什么是错的?

I tried this

sudo docker rm -f $(sudo docker ps -aq)

but get

"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
    
linux
ubuntu
docker
omega
omega
发布于 2017-11-15
2 个回答
Ravi
Ravi
发布于 2017-11-15
0 人赞同

我认为你没有任何预先存在的容器,这个命令的结果 sudo docker ps -aq 似乎是空的,这将导致总的命令为 sudo docker rm -f ,没有任何容器的ID。你可以跳过这个命令,因为没有预先存在的容器。

Phil E
Phil E
发布于 2017-11-15
0 人赞同

你把 sudo 的需要和另一个答案中提到的潜在的 "空 "容器列表之间的几个不同问题结合在一起。

另一个答案完全正确,这种命令的组合可能会导致 docker rm 的错误,因为 docker ps -aq 可能什么都不返回,使得 docker rm 命令没有选项,提示帮助文本。

当然,有两个原因,"内部 "命令可能什么都不返回。

  • there are actually zero running or exited containers; in this case you can ignore the error to docker rm , or run the docker ps -aq command by itself to convince yourself there are no containers returned.
  • The other reason is if the command failed due to lack of permission to talk to the Docker daemon. In your first example you show you are using sudo on the remove command, but not on the inner ps command, revealing the error that it could not talk to the docker socket. The output could be confusing because you are being shown two errors; one from each command: " Got permission denied... " is from the non-sudo version of docker ps and the second line " docker rm requires at least .. " is from docker rm not having anything to remove because the first command failed.
  •