Permanently delete a container in Docker
Normally, Docker does not delete stopped containers, it only unmarks them. This process occurs to avoid accidental deletion of stopped containers.
This article will discuss how to properly remove unused and untagged containers to reclaim resources in Docker.
Permanently delete a container in Docker
In Docker, we have remove command to forcefully remove a particular image. This command is for images docker rmi -f <image_id>
and for containers docker rm <container_id>
.
However, in theory, this command does not delete the container image, but only soft-deletes it by unmarking the object.
If we docker image ls -a
inspect our stopped images using the command , we can verify that the above command only unmarks our images instead of deleting them.
Sample output:
Untagged: my_image:latest
The following sections will discuss multiple scripts and parameters that will help us to permanently delete docker containers.
Pruning containers in Docker
When using Docker, we can quickly accumulate many unused objects that can take up a lot of disk space and clutter the output generated by Docker commands. Unfortunately, Docker does not remove untagged and unused objects such as containers, images, volumes, and networks unless we explicitly tell it to do so.
We can quickly reclaim resources by deleting stopped containers and pruning containers. For example, we can use the following command.
Sample code:
docker system prune -a
The -a flag will remove all unused images. After running it, we will be prompted with the following message.
We can bypass this message by adding the above command with the -f force flag.
WARNING! This command will remove the following:
- all containers with the stopped status
- all network paths not used by at least one (1) container
- all images without at least one (1) container associated with them
- all cache
Are you sure you want to continue? [y/N]
Additionally, the above command will not remove unused volumes attached to previously unused Docker containers. However, we can attach and use the --volumes parameter to include removal of unused volumes.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
Publish Date:2025/03/26 Views:97 Category:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
Publish Date:2025/03/26 Views:125 Category:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
Publish Date:2025/03/26 Views:202 Category:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
Publish Date:2025/03/26 Views:88 Category:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:140 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application