Docker bundle our software into standardized units called containers. Containers that have all the necessary elements which the software needs to run including libraries, system tools, code, and runtime.
When working with Docker, our disk space can rack up by large number of unused docker objects like containers, images, volumes and networks.
Now we are going to learn how to delete or remove containers in docker.
Remove all unused Objects in Docker:
The docker system prune command removes all unused containers, images and networks from Docker.
Syntax:
$ docker system prune
The above command shows the following output
Output:
Warning! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N]
press 'y' if you want to remove all unused objects.
Removing Docker Containers:
In Docker containers are not removed automatically. rm command is used to remove the containers in Docker.
1. Remove all unused docker container:
$ docker container prune
Output:
Warning! This will removes all stopped containers.
Are you sure you want to continue? [y/N]
press 'y' if you want to remove all unused containers.
2. Remove running containers :
To remove the specific container, the container name or container ID is needed. We get the container name and ID using the ls command.
ls command is used to list out the containers.
$ docker container ls
Output:
CONTAINER ID NAMES IMAGE COMMAND CREATED STATUS PORTS
4fff65e424a1 ubuntu "/bin/bash" 16 seconds ago Up 14 seconds ubuntu
Syntax:
$ docker rm
Example:
$ docker rm 4fff65e424a1
The above command get the error response, you can't remove a running container 4fff65e424a1. stop the container before attemting attemting removal or use -f.
There are 2 ways to remove the running container with rm command.
option 1: Using stop command before removing:
We have to stop the running container before removing as mentioned below.
$ docker stop 4fff65e424a1
$ docker rm 4fff65e424a1
Now the container 4fff65e424a1 has stopped and removed from the docker.
Option 2: Forcefully (-f) removing the container:
-f flag is used to remove the running container forcefully without any warning or error message if the specified container is available.
$ docker rm -f 4fff65e424a1
The above command remove the running container 4fff65e424a1 forcefully from the docker.
If you want to remove the docker image, just use the command below,
If you want to remove the docker image, just use the command below,
$ docker image rm ubuntu:latest
Comments (0)