Remove Docker containers
This article summarizes seven key points about deleting Docker containers.
It focuses on the docker container rm
command, and includes how to delete based on container status, as well as other content such as recovery methods and troubleshooting.
1. Docker container delete command [#1].
The docker container delete commands are as follows.
$ docker container rm [OPTIONS] CONTAINER [CONTAINER...]
This rm
command deletes one or more terminated containers.
You can execute it by specifying the container ID or name at the CONTAINER
position.
Note that only the container is removed, not the docker image.
The command to remove a docker image is docker image rm
, see this post for more details.
You can also add three options to the [OPTIONS] location: --force
, --link
, and --volume
. The --force
option is covered below in Section 4 .
The --link
option deletes the link containing the container. It is important to note that this does not delete the container itself.
Since Docker recommends using custom networks over using links, this option is likely to be deprecated. For now, we recommend using it only in legacy code.
The --volume
option means that when you delete a container, you also delete any volumes associated with it.
As we'll cover in a separate post, a volume that serves as storage for a container is an independent entity.
So when a container is deleted, the volume is not automatically deleted.
Let's check this out with an example. The following example gets a list of all containers, gets the container ID, and deletes it
$ docker container ls --all
CONTAINER ID IMAGE COMMAND ...
6f3e40d12122 nginx "/docker-entrypoint.…" ...
$ docker container rm 6f3e
6f3e
$ docker container ls --all
CONTAINER ID IMAGE COMMAND ...
You can see that the container has been successfully deleted and is no longer visible.
2. Alternatives for recovering deleted containers
Docker does not provide a way to recover containers that have been deleted with the rm
command. Therefore, in case of an unexpected deletion, it is good to be aware of the following alternatives.
2.1. Managing Data with Volumes
Docker volumes are the best way to securely store data that needs to be permanently stored in a container, or data that needs to be shared. As mentioned in Section 1, volumes are purged independently of containers, so make sure any data that shouldn't be purged from a container is stored in a volume.
2.2. Creating a new image using the docker container commit command
The docker container commit
command creates a new image with all changes in the current container.
So for containers that need to keep their current state, you can use this command periodically.
3. Workaround for Failing to Delete Containers
If your Docker container fails to drop, there are many possible causes. Here are five of the most common causes and their solutions.
3.1. Running containers
Docker prevents you from dropping a running container.
Therefore, it's a good practice to check if the container you want to delete is currently running, and if so, kill it with the docker container stop
command, and then delete it.
If you are sure that the container will not be damaged by being forcibly deleted, you can also delete it by following the instructions in Section 4 on how to forcibly delete a container.
3.2. Container being used by another container
You should also check if the container you want to delete is currently being used by another container, because if it is, you won't be able to delete it either.
A container can be linked to another container by a volume (--volumes-from
option), a link (--link
option), a network (--network
option), and so on.
In these cases, other containers using the current container must be deleted before the current container can be deleted.
3.3. Permissions problems
If the current user does not have the proper permission settings to use Docker, deleting a container might fail due to permission issues. Please see Stopping and Terminating a Docker Container - 7. What to do if permission denied occurs.
3.4 Other Server Environment Issues
In addition to the above, containers may be denied deletion if the server does not have enough disk space, or if the Docker daemon is not working properly. We recommend that you check these areas.
4. Forcing a running container to drop [#4].
Forcing the removal of a running container is not a recommended practice. However, if a container is not working properly and cannot be shut down normally, it may be necessary.
This is where the --force
option of the docker container rm
command comes in. You can use it like this
$ docker container rm --force CONTAINER
When forcibly deleting a container, always make sure that it will not affect other containers, volumes, etc. before proceeding.
5. Deleting all terminated containers
If all your terminated containers are no longer in use, there is a way to delete them all with a single command.
$ docker container prune
When you type this command, Docker will ask you again if you really want to delete all terminated containers. If you answer yes, it will proceed to delete all terminated containers.
As mentioned in Section 2, there is no way to recover deleted Docker containers, so use this command with caution.
6. Delete the entire container
I don't really recommend this, but if you really want to delete all your containers at once, whether they are running or stopped... there is a way.
$ docker container rm --force $(docker container ls -aq)
The docker container ls -aq
command looks up the container IDs in all states.
It sends this list of IDs as an argument to the rm
command in the $()
syntax of a shell script.
If any of the containers in the list are running, they will not be deleted naturally, so we need to add the --force
option to force them to be deleted.
7. Conclusion
In this article, we have summarized the information on how to delete a Docker container.
Focusing on the docker container rm
command, we covered how to delete a container based on its status, as well as recovery methods and troubleshooting.
I hope you find it useful when deleting a container.
