You are not logged in.

#1 2018-10-12 11:18:01

Registered: 2017-03-21
Posts: 145

[SOLVED]zshrc docker alias

I  am trying to get two commands run as an alias:

docker rm -f $(docker ps -a -q)


 docker rmi -f $(docker images -q)

Thats why i added this to my zshrc:

alias dockerclean='docker rm -f $(docker ps -a -q) && docker rmi -f $(docker images -q)'

i also added:

alias sudo='sudo '

Because i read that my shell only looks at the first word for an alias.
But when in run "sudo dockerclean" i get this error:

sudo dockerclean
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.38/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

Last edited by philipW (2018-10-13 23:51:17)


#2 2018-10-12 11:55:33

From: Netherlands
Registered: 2012-06-20
Posts: 808

Re: [SOLVED]zshrc docker alias

Hi philipW, instead of sudoing the alias, alias sudo too:

alias dockerclean='sudo docker rm -f $(docker ps -a -q) && sudo docker rmi -f $(docker images -q)'

well, to explain a little, you have the 'alias sudo='sudo ' so you can 'sudo alias' that will expand but;

your situation is -

 alias com='command && command'

so if you 'sudo alias' it will execute the first part as root.

But the command behind '&&' will not be expanded with sudo so you at least add that like

alias ='command && sudo command'

if you then 'sudo alias' it will execute both commands as sudo. Since there are 2 sudo's used in that alias, I would add them both, saves you typing 'sudo' everytime you execute the alias.

Clear enough wink I hope.

Last edited by qinohe (2018-10-12 13:03:27)


#3 2018-10-13 23:50:50

Registered: 2017-03-21
Posts: 145

Re: [SOLVED]zshrc docker alias

Hi qinohe,
Thanks for your reply i got it working now. It took me a while to find out i also had to put sudo in

$(docker ps -a -q)


$(docker images -q)'


Board footer

Powered by FluxBB