docker run: why use --rm (docker newbie)

matchingmoments picture matchingmoments · Apr 9, 2018 · Viewed 57.7k times · Source

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.

Answer

buddy123 picture buddy123 · Apr 9, 2018

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.