在 Docker 中永久删除容器
通常,Docker 不会删除已停止的容器,而只会取消标记它们。 发生此过程是为了避免意外删除已停止的容器。
本文将讨论如何正确删除未使用和未标记的容器以回收 Docker 中的资源。
在 Docker 中永久删除容器
在 Docker 中,我们有 remove 命令可以强制删除特定图像。 此命令是用于图像的 docker rmi -f <image_id>
和用于容器的 docker rm <container_id>
。
然而,在理论上,该命令不会删除容器镜像,而只会通过取消标记对象来软删除它。
如果我们使用命令 docker image ls -a
检查我们停止的图像,我们可以验证上述命令只会取消标记我们的图像而不是删除它们。
示例输出:
Untagged: my_image:latest
以下部分将讨论多个脚本和参数,以帮助我们永久删除 docker 容器。
在 Docker 中修剪容器
使用 Docker 时,我们可以快速积累许多未使用的对象,这些对象会占用大量磁盘空间并使 Docker 命令生成的输出变得混乱。 不幸的是,除非明确告诉我们这样做,否则 Docker 不会删除未标记和未使用的对象,例如容器、图像、卷和网络。
我们可以通过删除停止状态的容器,通过容器剪枝来快速回收资源。 例如,我们可以使用下面的命令。
示例代码:
docker system prune -a
-a 标志将删除所有未使用的图像。 运行后会提示我们如下信息。
我们可以通过添加带有 -f force 标志的上述命令来绕过此消息。
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]
此外,上述命令不会删除附加到以前未使用的 Docker 容器的未使用卷。 但是,我们可以附加并使用 --volumes 参数来包括删除未使用的卷。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类: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
发布时间:2025/03/26 浏览次数:165 分类: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
发布时间:2025/03/26 浏览次数:131 分类: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
发布时间:2025/03/26 浏览次数:107 分类: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
发布时间:2025/03/26 浏览次数:97 分类: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
发布时间:2025/03/26 浏览次数:124 分类: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
发布时间:2025/03/26 浏览次数:202 分类: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
发布时间:2025/03/26 浏览次数:87 分类: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
发布时间:2025/03/26 浏览次数:140 分类: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