How to stop and remove a docker container?

I'm trying to remove a docker/image/container and all stuff which is somehow connected with docker, but first of all I need to stop it. While I'm trying to execute this command:

docker stop $(docker ps -a -q)

It gives me an error:

[:/home/imran] 1 $ docker stop $(docker ps -a -q)

2015/01/16 00:37:38 Get http:///var/run/docker.sock/v1.14/containers/json?all=1: dial unix /var/run/docker.sock: permission denied

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

Stop a running container by sending SIGTERM and then SIGKILL after a grace period

  -t, --time=10      Number of seconds to wait for the container to stop before killing it. Default is 10 seconds.

How can I uninstall a docker container?


I've fixed it! Please don't forget - all your data in the containers will be removed!

So, first of all we need to execute this commands:

# adding new group
$ sudo groupadd docker

# adding user to the 'docker' group
$ sudo gpasswd -a ${your_username} docker

# restart the docker (documentation suggests to use 'docker.io' instead of 'docker',
# for me both variants work just fine!
$ sudo service docker restart

Then we need to logout, DON'T use GUI-variant because it didn't work for me and i was dissapointed about this.

Instead, use this command:

sudo pkill -u username

Then we need to...

1. Kill all running containers

sudo docker kill $(docker ps -q)

2. Delete all stopped containers

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

3. Delete all 'untagged/dangling' images

sudo docker rmi $(docker images -q -f dangling=true)

4. Delete all images

sudo docker rmi $(docker images -q)

Sources:
https://www.calazan.com/docker-cleanup-commands/
http://www.linuxquestions.org/questions/ubuntu-63/how-do-i-log-out-via-terminal-928183/

P.S. Maybe other answers are correct too, but at the moment these answers were published my problem was already fixed and I'm unable to check if they are correct or not. Thanks to @Andreas. He pointed out a mistake that containers were already removed. Since I didn't find any correct and "all in one" sollution I wanna tell you how you can fix it.


You need to execute these commands as root, i.e. using sudo:

sudo docker stop $(sudo docker ps -a -q)

Or:

sudo sh -c "docker stop $(docker ps -a -q)"