131

I am trying docker for the first time and do not yet have a "mental model". Total beginner.

All the examples that I am looking at have included the --rm flag to run, such as

docker run -it --rm ...
docker container run -it --rm ...

Question:

Why do these commands include the --rm flag? I would think that if I were to go through the trouble of setting up or downloading a container with the good stuff in it, why remove it? I want to keep it to use again.

So, I know I have the wrong idea of docker.

425nesp
  • 5,124
  • 4
  • 40
  • 56
matchingmoments
  • 1,507
  • 2
  • 6
  • 7
  • 8
    `--rm` removes the stopped container (the one `docker run` creates), not the image it was based on (what you downloaded). – Ry- Apr 09 '18 at 05:46
  • ```docker run --help``` should be useful. If you don't want remove **container**, you can pass it – indapublic Apr 09 '18 at 05:46
  • 1
    it must be `--rm` and it's to automatically clean up the container and remove the file system when the container exits and it doesn't mean removing the stuff from the container. – Mahattam Apr 09 '18 at 06:04
  • 2
    You don't download containers, you download images. Read here to learn the differences between images and containers: https://docs.docker.com/engine/docker-overview/#docker-objects – axiac Apr 09 '18 at 06:23

5 Answers5

115

Containers are merely an instance of the image you use to run them. The state of mind when creating a containerized app is not by taking a fresh, clean ubuntu container for instance, and downloading the apps and configurations you wish to have in it, and then let it run.

You should treat the container as an instance of your application, but your application is embedded into an image. The proper usage would be creating a custom image, where you embed all your files, configurations, environment variables etc, into the image. Read more about Dockerfile and how it is done here

Once you did that, you have an image that contains everything, and in order to use your application, you just run the image with proper port settings or other dynamic variables, using docker run <your-image>

Running containers with --rm flag is good for those containers that you use for very short while just to accomplish something, e.g., compile your application inside a container, or just testing something that it works, and then you are know its a short lived container and you tell your Docker daemon that once its done running, erase everything related to it and save the disk space.

jordiburgos
  • 4,742
  • 3
  • 38
  • 65
buddy123
  • 4,657
  • 8
  • 42
  • 65
  • Would passing the environment variables to docker run be a better alternative. This way you can start with different configurations (eg. production versus preproduction) ? – SCO Apr 09 '18 at 09:44
  • Prod vs Test environment is a good example where you would probably want to use env vars to make the separation and get your app to run from the same image, but with different settings for each env – buddy123 Apr 09 '18 at 11:05
  • Is there a way to automatically delete the containers that are started using `--rm` when the user closes the terminal window. i.e not deliberately run `exit` in the container shell, but just closes the terminal GUI window. I've noticed that containers are not deleted in this case. – venkrao Jun 09 '20 at 10:09
27

The flag --rm is used when you need the container to be deleted after the task for it is complete.

This is suitable for small testing or POC purposes and saves the headache for house keeping.

jordiburgos
  • 4,742
  • 3
  • 38
  • 65
Adnan Khan
  • 461
  • 3
  • 5
21

From https://docs.docker.com/engine/reference/run/#clean-up---rm

By default a container’s file system persists even after the container exits. This makes debugging a lot easier (since you can inspect the final state) and you retain all your data by default. But if you are running short-term foreground processes, these container file systems can really pile up. If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the --rm flag

In short, it's useful to keep the host clean from stopped and unused containers.

Pang
  • 8,605
  • 144
  • 77
  • 113
1

I use --rm when connecting to running containers to perform some actions such as database backup or file copy. Here is an example:

docker run -v $(pwd):/mnt --link app_postgres_1:pg --rm postgres:9.5 pg_dump -U postgres -h pg -f /mnt/docker_pg.dump1 app_db

The above will connect a running container named 'app_postgres_1' and create a backup. Once the backup command completes, the container is fully deleted.

Shoan
  • 3,818
  • 1
  • 24
  • 28
1

When you run a container from an image using a simple command like (docker run -it ubuntu), it spins up a container. You attach to your container using docker attach container-name (or using exec for different session).

So, when you're within your container and working on it and you type exit or ctrl+z or any other way to come out of the container, other than ctrl+p+q, your container exits. That means that your container has stopped, but it is still available on your disk and you can start it again with : docker start container-name/ID. But when you run the container with —rm tag, on exit, the container is deleted permanently.

Plabon Dutta
  • 4,101
  • 3
  • 22
  • 29