The --rm Flag in Docker

David Mbochi Njonge Nov 21, 2022
  1. Pull an Nginx Image
  2. Run a Container From the Image
  3. Conclusion
The --rm Flag in Docker

Generally, when most developers start using docker, removing a container after going through all the processes of pulling an image, building an image, and running the container defeats the original purpose of doing it. However, for experienced developers, this comes with benefits that help develop applications and make them as efficient as possible.

This tutorial will discuss using the rm command in docker. In addition, you will also learn the advantages of using this command.

Pull an Nginx Image

In this tutorial, you will use an Nginx image. You can also use any image from the docker hub.

Therefore, open a new terminal(keyboard shortcut Ctrl+Alt+T) and use the below command to pull the image.

~$ docker pull nginx:alpine

Output:

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

Run a Container From the Image

Once the image has been downloaded, use the below command to run a container with the name temp-container.

~$ docker run --rm --name temp-container -w /com/app/ nginx:alpine pwd

Output:

/com/app

In the docker run command, you have used the --rm flag, which automatically removes the container when it exits. You have also used the -w flag to set the working directory in the container.

Furthermore, the working directory for the container was printed using the pwd command. Generally, the command runs the container, sets the container’s working directory, and removes the container by the --rm flag after printing the working directory.

Since the main objective of running this container is to test whether the working directory is set successfully, there is no need to have the container on the host, thus the container’s deletion.

This is one of the use cases of the --rm flag. The benefit of using this command is that you can conserve computer storage that is used by unused containers.

To verify that this command was removed, use the below command to check if the container exists.

~$ docker ps -a

Output:

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

If you have other running containers in the container, they will all be listed using the above command. However, nothing will be listed on the console if you do not have other containers.

Lastly, another advantage of using the --rm flag is that it helps us to automatically perform clean-up and proof of concept.

Conclusion

Only use this command for short-lived containers, especially those used for testing purposes. In conclusion, this article has shown you how to use the --rm flag and the reasons for using this command to run containers.

Finally, you have learned some benefits one gain from using this command.

David Mbochi Njonge avatar David Mbochi Njonge avatar

David is a back end developer with a major in computer science. He loves to solve problems using technology, learning new things, and making new friends. David is currently a technical writer who enjoys making hard concepts easier for other developers to understand and his work has been published on multiple sites.

LinkedIn GitHub

Related Article - Docker Command