If you are a Docker administrator and are responsible for managing a Docker container, then you may need to remove unwanted Docker containers from your system. Removing a Docker container is not a difficult task. Docker provides a useful command-line option to clean up your system.

This tutorial will explain some useful commands that you can use to remove Docker containers and keep your system organized.

Remove a Single Container

To remove a Docker container, you will need to stop the container if it is in the running state.

First, locate the ID of all containers via the following command:

Or

You should see a list of the IDs of all containers in the following output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ebed89569170 nginx “https://linuxhint.com/docker-entrypoint.…” 44 seconds ago Up 42 seconds 80/tcp gifted_snyder

0b5c6967b76f mariadb “docker-entrypoint.s…” 56 seconds ago Exited (1) 54 seconds ago suspicious_napier

8f53ea801780 wordpress “docker-entrypoint.s…” About a minute ago Up About a minute 80/tcp crazy_nobel

ae6cc9f3a1cf wordpress “docker-entrypoint.s…” 2 minutes ago Exited (0) 2 minutes ago lucid_wozniak

Now, run the following command by specifying the ID of the container that you want to stop:

Or

docker container stop ebed89569170

Once your container is stopped, run the following command to remove the container:

Remove Multiple Containers

You can also remove multiple containers at a time using a single command.

To remove multiple containers, run the following command, along with the IDs of the containers that you want to remove:

Remove All Containers

Docker also allows you to remove all containers on your system at once using a single command. But you must first stop all running containers on your system.

First, stop all the containers from running with the following command:

docker stop $(docker ps -aq)

You should see the following output:

713f999fb332


0a51419388e2


0b5c6967b76f


8f53ea801780


ae6cc9f3a1cf

Next, make sure that all the containers are stopped using the following command:

You should see the following output if all the containers have been stopped:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Next, remove all the containers by running the following command:

docker rm $(docker ps -aq)

Verify whether all containers are deleted using the following command:

You should see the following output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Conclusion

In the above guide, you learned how to remove Docker containers. I hope this will help you to perform day-to-day tasks on your system.