Docker 中的 --rm 标志
通常,当大多数开发者开始使用 docker 时,在经历了拉取镜像、构建镜像、运行容器的所有过程后,移除一个容器就违背了这样做的初衷。 然而,对于有经验的开发人员来说,这带来了帮助开发应用程序并使它们尽可能高效的好处。
本文将讨论在 docker 中使用 rm 命令。 此外,您还将了解使用此命令的优势。
拉取一个 Nginx 镜像
在本教程中,您将使用 Nginx 图像。 您还可以使用来自 docker hub 的任何图像。
因此,打开一个新的终端(键盘快捷键 Ctrl+Alt+T)并使用下面的命令来拉取图像。
~$ docker pull nginx:alpine
输出:
alpine: Pulling from library/nginx
ca7dd9ec2225: Already exists
76a48b0f5898: Pull complete
2f12a0e7c01d: Pull complete
1a7b9b9bbef6: Pull complete
b704883c57af: Pull complete
4342b1ab302e: Pull complete
Digest: sha256:455c39afebd4d98ef26dd70284aa86e6810b0485af5f4f222b19b89758cabf1e
Status: Downloaded newer image for nginx:alpine
docker.io/library/nginx:alpine
从镜像运行容器
下载图像后,使用以下命令运行名为 temp-container 的容器。
~$ docker run --rm --name temp-container -w /com/app/ nginx:alpine pwd
输出:
/com/app
在 docker run
命令中,您使用了 --rm 标志,它会在容器退出时自动删除它。 您还使用了 -w 标志来设置容器中的工作目录。
此外,容器的工作目录是使用 pwd 命令打印的。 通常,该命令运行容器,设置容器的工作目录,并在打印工作目录后通过 --rm 标志删除容器。
由于运行这个容器的主要目的是测试工作目录是否设置成功,所以宿主机上不需要有容器,所以删除了容器。
这是 --rm 标志的用例之一。 使用此命令的好处是您可以节省未使用的容器所使用的计算机存储空间。
要验证此命令是否已删除,请使用以下命令检查容器是否存在。
~$ docker ps -a
输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
如果容器中有其他正在运行的容器,它们将使用上述命令全部列出。 但是,如果您没有其他容器,则控制台上不会列出任何内容。
最后,使用 --rm 标志的另一个优点是它可以帮助我们自动执行清理和概念验证。
总结
仅将此命令用于短期容器,尤其是用于测试目的的容器。 总之,本文向您展示了如何使用 --rm 标志以及使用此命令运行容器的原因。
最后,您了解了使用此命令的一些好处。
相关文章
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