How to Stop, Remove and Clean up Docker

Are you new to docker? then running and maintaining docker applications is easy. This article explains how to stop docker container applications using docker stop, docker-compose stop and docker kill. docker-compose comes handy when managing a complex multi container docker application.

If you have docker services or images running on your system you can verify by executing

docker ps

To see all of the containers even if they are not running execute add a -a
If you need to to attach to a shell within the running container, run

docker attach [container id]

If docker attach never connects, run docker exec -i -t [container id] /bin/bash

1) Stopping a Docker container by ID

If you need to stop the container, run

docker stop [container id]

You can verify it stopped by running

docker ps -a

In a multi container environment services can be stopped by running

docker-compose stop

If you are done with the container and ready to delete it, run

docker rm [container id]

Example

This example will download the mysql image, create a mysql database and expose it to your local machine to interact with.

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_USER=mysql_user -e MYSQL_PASSWORD=mysql_user1 -e MYSQL_DATABASE=mysql_test -p 3306:3306 -d mysql

  • –name: is the name to give the container
  • -e: sets environment variables
  • -d: runs container in the background
  • -p: set the port to forward to.

If you need to to attach to a shell within the running container, run

docker attach [container id]

If docker attach never connects, run docker exec -i -t [container id] /bin/bash

If you need to stop the container, run

docker stop [container id]

You can verify it stopped by running

docker ps -a

If you are done with the container and ready to delete it, run

docker rm [container id]

Stopping a Docker container by name

Docker containers can also be stopped  if the information you have is the image name and you want to find all matching running containers of that image name and stop them. This can be done by running

docker ps -a -q --filter="name=<containerName>"

2) Stopping a Docker daemon

When running docker applications, the daemon runs as it's default configuration. In a production environment however, system admins typically configure the docker daemon to start and stop according to the organization's requirements. To view the status of the docker daemon execute

sudo status docker

You can then stop the Docker daemon by using

sudo stop docker

3) Stopping multiple containers

In this section we will see how to stop multiple containers in a system. There may arise situations where you may be required to stop all running containers either due to server overload, security breaches or good old maintenance. Stopping all docker containers is pretty easy. The easiest way will be by running

docker kill $(docker ps -q)

4) Removing containers

Once docker containers are stopped you may need to delete docker images. One thing most system administrators may find very annoying is all unused containers and images taking up precious space. It would have been easier if there was a  docker cleanup command to do the job. Perhaps that may happen in the future.
remove all containers

docker rm $(docker ps -a -q)

remove all docker images

docker rmi $(docker images -q)

Remove one or more specific images
Use the docker images command with the -a flag to locate the ID of the images you want to remove. This will show you every image, including intermediate image layers. When you've located the images you want to delete, you can pass their ID or tag to docker rmi:

docker images -a

Remove:

docker rmi image_one image_two

Remove dangling images

Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space. They can be located by adding the filter flag, -f with a value of dangling=true to the docker images command. When you're sure you want to delete them, you can add the -q flag, then pass their ID to docker rmi:

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

Removing images according to a pattern

You can find all the images that match a pattern using a combination of docker images and grep. Once you're satisfied, you can delete them by using awk to pass the IDs to docker rmi. Note that these utilities are not supplied by Docker and are not necessarily available on all systems:

docker images | grep "pattern" | awk '{print $1}' | xargs docker rm

Removing Volumes

Use the docker volume ls command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm command:

docker volume rm volume_name volume_name

Remove a container and its volume

If you created an unnamed volume, it can be deleted at the same time as the container with the -v flag. Note that this only works with unnamed volumes. When the container is successfully removed, its ID is displayed. Note that no reference is made to the removal of the volume. If it is unnamed, it is silently removed from the system. If it is named, it silently stays present.

Remove:

docker rm -v container_name

Conclusion

This article covers the fundamentals of stopping and removing docker images, containers and volumes. There are many other combinations and flags which can be used in each. For more comprehensive list, please review the docker documentation. However if there are any aspects you would like to see in this guide, please ask questions or make suggestions in the comments section.

About Nyah Check

Founder of Buea Docker Meetup and Golang Buea (Buea Gophers Meetup). Interested in Cloud and Container technologies.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.