Remove a Docker container

Earlier, we covered how to Run a Docker container & Stop and Start a Docker container. The next part – how to remove a Docker container? So, we run a Docker container and then start and stop it as and when necessary. But, at times certain containers are no longer needed. They would have either reached end of life or could have fulfilled their purpose.

In such cases, it is better to remove the containers. Otherwise, they would take unnecessary storage space. And, if too many of such containers we don’t remove then we may have to look for additional storage space as well.

We can easily remove Docker containers by identifying them either through Container ID or Container Name. Get the required data about a container through the following command –

docker container ls -a

where,

-a option is used to list all containers irrespective of their status.

Remove a Docker container

Use the following syntax to remove a Docker container –

docker container rm <Container-ID>

or,

docker container rm <Container-Name>

Besides, if we can’t remove a container for some reason then if needed we may have to forcefully remove it. For that, we can use -f option. We advise you to go with -f option only if you’re sure that there is no other way left. The reason why we write that is, the container could be running and we inadvertently remove it with -f option. In that case, the right approach is to first stop the container and then remove it.

docker container rm -f <Container-ID>
docker container rm -f <Container-Name>

In short, identify the container which we want to remove. Copy its Container Name or ID. Use docker container rm command as described above to remove it.

For instance,

docker container rm f09661f6b8e5

or,

docker container rm jovial_almeida

In conclusion, we have covered how to remove a Docker container here. Removing a container frees up precious system resources which can be put to use someplace else.

Similar Posts