A docker container is a running instance of a docker image. This docker tutorial will teach us the commands to remove docker containers with simple examples.
1. Why Remove Docker Containers?
We should remove the docker containers if these containers are unused for a long time. It helps to release the unnecessarily occupied resources that can be used by the other running containers.
2. Listing All Containers
Using the following
ps commands, we can check the available containers (running and non-running). The first command shows only the running containers while the use with flag
-a displays all the available containers, including non-running ones.
$ docker ps // show running containers $ docker ps -a // show all containers(running and non-running)
Initially, we don’t have any running containers in docker, so it shows an empty list, but after adding containers, if we run the same command, docker shows the container ids and names, as follows:
3. Removing a Container by Id or Name
Removing a container is quite easy, we just need to call
'docker rm' command. We can use either container name or id to remove any container. We can get container info by using the docker ps command. Here, we are removing a container by its id.
$ docker rm <container-id> or <container-name>
4. Remove a Container upon Exit
A container after completing its task gets stopped but remains in the docker memory, so if we want to remove a container upon exit then we can set –rm flag with the
docker run --rm docker_image_name
5. Removing Containers by Date Range
We can remove docker containers based on the date and time. For example, if we want to remove all the containers created 15 minutes ago then we can use until filter with the docker prune command like below:
$ docker container prune --filter "until=15m"
We can also specify a date with this prune command. For example, to remove containers created before 2022/08/30, we can use the below command:
$ docker container prune --filter "until=2022-08-30"
We can even specify the time with the date as:
$ docker container prune --filter "until=2022-08-30T16:30:00"
This command will remove all the containers of the date till 04:30 pm (12 hours time format).
6. Pruning All Stopped Containers
We can remove all the stopped or unused containers from the docker by using the below command.
$ docker container prune
For this command, docker shows a warning message and asks for user input to confirm the command. Remember, this will remove all the stopped containers only, not the running.
7. Forcefully Stop and Remove All Containers
To stop a container, we can use the docker stop command like:
$ docker stop <container-name>
To remove containers forcefully, we can use the –force flag with the docker rm command.
// forcefully remove single container $ docker rm --force <container-name> // forcefully remove all the stopped containers $ docker rm --force $(docker ps --filter status=exited -q)
If you don’t want to filter containers based on the exit status rather remove all the stopped containers forcefully then can use the below prune command:
$ docker container prune --force
This article explains the useful commands to remove running and stopped docker containers. You can use these commands for better memory utilization by removing unused containers individually or cleaning up the all.
Happy Learning !!
Leave a Reply