You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Docker新手求教:`--rm`参数的作用及示例为何使用它?

Why do Docker run commands include the --rm parameter?

Great question—this is such a common point of confusion when you're new to Docker, so let's clear this up with some basics first, since it sounds like you might be mixing up two key Docker concepts: images and containers.

First, let's get that distinction straight:

  • An image is the reusable, read-only template you download (like the Ubuntu or Node.js images you pull from Docker Hub). This is what you're "investing" time in downloading, and it sticks around on your system unless you explicitly delete it.
  • A container is a running (or stopped) instance of an image. Think of it as a temporary, isolated environment created from the image. Any changes you make inside a container (like installing software or editing files) only exist in that specific container—unless you're using volumes to persist data.

Now, what does --rm do?
The --rm flag tells Docker to automatically delete the container instance as soon as it stops running. Crucially, it does NOT delete the underlying image—your downloaded template is still safe and available to use again.

So why would you want to delete a container after it stops?

  • Temporary tasks: If you're running a one-off command (like docker run --rm node:latest npm install to run a package install without setting up Node on your host), you don't need the container hanging around afterward. --rm cleans it up automatically, so you don't end up with dozens of stopped containers cluttering your system.
  • Interactive testing: If you're just poking around an image (e.g., docker run -it --rm ubuntu to explore the Ubuntu command line), you probably don't need to save that session. Exiting the shell will delete the container, but the Ubuntu image is still there if you want to spin up a new one later.

When should you NOT use --rm?

  • If you want to persist changes or state in the container: For example, if you install a web server and configure it inside a container, and you want to start that exact same container again later, skip --rm. You can restart stopped containers with docker start <container-id> and access them with docker exec.
  • If you're using volumes to persist data (though even then, if the container itself is disposable, --rm is still safe—volumes are separate from containers and won't be deleted).

To address your core concern: You're not wasting the time you spent downloading the image by using --rm! The image stays on your system, so you can spin up new containers from it anytime without re-downloading. The only thing --rm removes is the temporary container instance once it's done its job.

If you ever forget to use --rm and end up with stopped containers cluttering your system, you can clean them up manually with:

# Delete a specific stopped container
docker rm <container-id>

# Delete all stopped containers at once
docker container prune

内容的提问来源于stack exchange,提问作者matchingmoments

火山引擎 最新活动