Docker Part 2 – HOWTO Remove / Delete Docker Containers

So you have been messing with docker for a few minutes or hours, and now you have a bunch of either running or stopped containers you no longer need. How do you get rid of them?

Removing Single Containers

To remove a single docker container, you simply start by listing all of the docker containers (started or stopped) to ensure you know which one to delete:

$ sudo docker ps –a


Then remove the chosen container:

$ sudo docker rm <container name>


If the container is currently running you can simply add –f to stop and remove the container in a single command:

$ docker rm -f <container name>


Unless it’s paused, then you will get an error something like the following:

Error response from daemon: Could not kill running container, cannot remove - Container e4f28eccb0cbcfbf4d78104bfe3e84039f62c5073f7301f8a39bb77a9598ae72 is paused. Unpause the container before stopping


This is easy to resolve. The “docker pause” command was added as of Docker 1.0, allowing for better resource utilisation if you have containers you don’t currently need to be wasting CPU cycles. As of Docker 1.1, running containers are also paused during commit activities, to ensure a consistent file system. Simply check the ID of the VM (with a ps command), unpause it, then remove:

sudo docker ps
sudo docker unpause <container id>
sudo docker rm -f <container id>

 

Removing Multiple Containers

Sometimes we have built up a number of containers and we just want to scrub the lot in one go. If you want to remove all containers (running or not), first you need to generate a list of all of the container IDs, then you pass that list to the docker rm command as follows:

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


Alternatively if you wish to remove only the non-running containers:

sudo docker rm $(sudo docker ps -q)

 

That’ll do for now, but in the next post I will go into how to install your first app…

http://www.tekhead.org/blog/2016/02/docker-part-3-howto-create-a-simple-python-web-app-in-docker/

Docker , , , , , , , , , ,