In Docker, you can remove a container using the docker rm
command. This command deletes the specified container from your system.
The following example shows how to remove a Docker container.
How to Remove a Stopped Docker Container
To remove a stopped Docker container, we can use the docker rm
command followed by the container ID or name.
we can use the docker ps -a
command to get the list of all Docker containers (both running and stopped). It returns the container ID, name, command, and other details.
w ecan use either the container ID or name to retrieve the container’s IP address.
Suppose we want to remove a container app-nginx
, we can run the following command in the terminal:
docker rm app-nginx
Output: 👇️
In this example, the docker rm
command removes the container named app-nginx
How to Remove a Running Docker Container
To remove a running Docker container, we need to stop it first using the docker stop
command and then remove it using the docker rm
command.
Suppose we want to remove a running container named app-nginx
, we can use the following commands in the terminal:
docker stop app-nginx docker rm app-nginx
Output: 👇️
app-nginx
app-nginx
In this example, the docker stop
command stops the container and the docker rm
command removes it.
If we try to delete a running container, it will throw an exception as given below
Error response from daemon: cannot remove container "/app-nginx": container is running: stop the container before removing or force remove
How to Force Remove a Docker Container
To force remove a running Docker container without stopping it first, we can use the -f
flag with the docker rm
command.
Suppose we want to force remove a container named app-nginx
, we can run the following command in the terminal:
docker rm -f app-nginx
Output: 👇️
app-nginx
In this example, the docker rm -f
command forcefully removes the container named app-nginx
.
Conclusion
We can remove Docker containers using the docker rm
command.
This allows us to clean up and manage our Docker environment by deleting no longer-needed containers.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.