Updated March 28, 2023
Introduction to Docker Delete Container
Docker rm is a Docker command used to delete or remove one or more containers. In order to remove the container, it should be in the stopped state; however, we can forcefully remove a running container using the ‘-f’ flag. We need a container ID or container name to remove the container. When we remove the container, all data of the container get destroyed, so it is recommended to use volume for persistent storage. In this topic, we are going to learn about Docker Delete Container.
$docker rm --help
-f, –force: This option is used to remove a running container. It is going to use SIGKILL to kill the process of running that container.
-l, –link: It is used to remove the link
-v, –volumes: It is used to remove anonymous volumes attached to the container
How to Delete Container in Docker?
In order to delete one or more containers in Docker, we run the ‘docker rm’ command from Docker CLI, and the Docker client makes an API call to the docker daemon to delete the container. Next, the Docker daemon checks for that container and checks its status; if the status is running, the Docker daemon will throw an error that the container is running and suggest using the ‘-f’ flag to forcefully delete the container. If the container is deleted successfully, the docker daemon output the specified container name or container ID on the STDOUT.
Examples of Docker Delete Container
Different examples are mentioned below:
We will create a ngninx and an ubuntu container and will try to delete it.
- Create an nginx container using the below command: –
$docker run -d nginx
- Create an ubuntu container as below: –
$docker run -d ubuntu
- List all containers: –
$docker ps –a
Explanation: In the above example, the first container named ‘modest_khorana’ is in exited status, and the second container named ‘serene_hodgkin’ is up, which means it is in a running state. Let’s try to remove the containers using the ‘docker rm’ command.
- Delete or remove the stopped container first as below: –
$docker rm <container_ID>
$docker rm <container_name>
$docker rm d077d5f4873e
We can see the container is removed successfully.
- Now, let’s try to rm the running container using the same command: –
$docker rm 980c7d53a4e1
Explanation: In the above snapshot, we can see we got an error that the Docker daemon cannot remove a running container; either we need to stop the container or force remove it.
- Here, we are going to forcefully remove the container using the ‘-f’ flag as below: –
$docker rm -f
Explanation: In the above snapshot, we can see that the container has been deleted successfully without any error, and no container exists now.
Below is the single snapshot of all commands executed above:
We will create a linked container and will delete the linked containers.
- Create a source container named ‘my-db’ using ‘mysql’ Docker image as below: –
$docker run -d --name my-db mysql sleep 3600
- Now, link the ‘my-db’ container with alias ‘db’ to the new container named ‘my-web’ as below: –
$docker run -d -P --name my-web --link my-db:db nginx
- We will now delete the containers under this link as below: –
$docker rm -f /my-web
$docker rm -f /my-db
- If we have to only remove the link between those linked containers, we can use the ‘–link’ or ‘-l’ flag to remove the link on the default bridge network as below: –
$docker rm -l /my-web/db
Explanation: In the above snapshot, we can see that containers have been recreated as the container was deleted in the last step and then removed the link between the containers; however, it does not delete the containers as you can see in the snapshot, containers are still running.
We will run a container with an anonymous volume attached to it and delete the container with the anonymous volume using the ‘-v’ flag.
- Create an alpine container with an anonymous volume attached to it as below: –
$docker run -d -v /root alpine
- Let’s delete the container without any flag as below: –
$docker rm 3f04
Explanation: – In the above example, if we remove the container without any flag, we could see that the anonymous volume is still present. Let’s use the ‘-v’ flag to remove the anonymous volume along with the container using the below command: –
$docker rm -v 39b7
Explanation: – In the above snapshot, we have recreated the container with anonymous volume; however, we have used the ‘-v’ flag while removing the container, which also removed the anonymous volume attached to this container.
Note: The ‘-v’ flag only removes the anonymous volume attached to the container, not the named volume. For example, we have a named volume ‘my-vol’ and attached to the container; then it will remain intact as shown below: –
$ docker run -d -v my-vol:/etc -v /root alpine
Explanation: In the above snapshot, we can see that the volume ‘my-vol’ is still there besides using the ‘-v’ flag.
Assume we have multiple containers in the stopped state or the excited state, and we want to get rid of it and want to delete all containers in a single shot.
- Let’s create 3 alpine containers and an nginx container as below: –
$docker run -d alpine
Note: run the above command 3 times to create 3 containers
$docker run -d alpine
- Let’s check the status of the container using below command: –
$docker ps –a
- The below command is used to remove all stopped containers: –
$docker rm $(docker ps -a -q)
Note: Use the ‘-f’ flag if we want to delete all containers.
Explanation: In the above snapshot, we see that the command $(docker ps -a -q) queried all containers and send it to the remove command to delete; however it throws an error for the running container. So we have one more command to remove all the containers except the running one.
- Let’s create a few containers once again as above and delete the stopped containers using the below command: –
$docker container prune
Explanation: – In the above snapshot, we have 3 containers in exited state and 1 container in running state, and after using the ‘prune’ command, all stopped containers have been removed except the running one, and it did not throw any error for the running container.
We use the ‘docker rm’ command to delete or remove the containers that are no longer required; however, we have an alternative command, ‘docker container rm,’ that works similar to the ‘docker rm’ command. It also has the same flags. We can use any of the commands as per our comfortability.
This is a guide to Docker Delete Container. Here we discuss How to Delete Container in Docker and Examples along with the syntax and explanation. You may also have a look at the following articles to learn more –