Docker

How to Remove Docker Containers

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:

docker ps -a

Or

docker container ls -a

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 "/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:

docker stop ebed89569170

Or

docker container stop ebed89569170

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

docker rm ebed89569170

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:

docker rm ID1 ID2 ID3

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:

docker ps

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:

docker ps -a

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.

About the author

Hitesh Jethva

Hitesh Jethva

I am Hitesh Jethva lives in Ahmedabad, INDIA. I am Linux system administrator and Technical writer.

I felt in love with Linux while i was started to learn Linux. I am a fan of open source technology and have more than 8+ years of experience in Linux and Open Source technologies. My main motto is to make uncomplicated things easier.

I have extensive experience within the following areas: CentOS/RHEL/FreeBSD/Ubuntu/Debian, cPanel/WHM, High Availability Architecture, Proactive security/Web Application Security, Amazon Web Services (AWS), Apache/Nginx, OpenLDAP, DevOps automation: Chef/Docker.

My expertise includes Linux system administration, installation, configuration, tuning, security and troubleshooting. I have large skills in configuring/management/support of Apache, Nginx, MariaDB, MongoDB, PHP-FPM, Nagios, Zabbix, Graylg, Redis, Docker and much more.