232

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.

4
  • 35
    --rm removes the stopped container (the one docker run creates), not the image it was based on (what you downloaded).
    – Ry-
    Commented Apr 9, 2018 at 5:46
  • 1
    docker run --help should be useful. If you don't want remove container, you can pass it
    – indapublic
    Commented Apr 9, 2018 at 5:46
  • 2
    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
    Commented Apr 9, 2018 at 6:04
  • 3
    You don't download containers, you download images. Read here to learn the differences between images and containers: docs.docker.com/engine/docker-overview/#docker-objects
    – axiac
    Commented Apr 9, 2018 at 6:23

6 Answers 6

203

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 it's a short lived container and you tell your Docker daemon that once it's done running, erase everything related to it and save the disk space.

3
  • 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
    Commented Apr 9, 2018 at 9: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
    Commented Apr 9, 2018 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
    Commented Jun 9, 2020 at 10:09
82

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.

39

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.

4

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.

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.

0

The "docker run rm " command makes us run a new container and later when our work is completed then it is deleted by saving the disk space.

The important thing to note is, the container is just like a class instance and not for data storage. We better delete them once the work is complete. When we start again, it starts fresh.

The question comes then If the container is deleted then what about the data in a container? The data is actually saved in the local system and get linked to it when the container is started. The concept is named as "Volume or shared volume".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.