Docker

Automatically Removing the Container

Benefits of automatically removing the container

  1. It helps in keeping the system free of garbage.
  2. In production environments, sometimes we just need to test the final code, we do not need to actually deploy it, in such conditions it is helpful.

Question: Is there any way with which we can remove or delete the container on log out?

Answer: Yes, docker also provides a dedicated argument with the help of which it is possible to automatically remove the container on exit or log out.

We are actually going to create a container based on the “Ubuntu” image and we want to run the “bash”, for the root prompt in order to execute commands. For confirmation, we have executed “hostname command” to get the hostname of the system and it is returning the name as shown in the image below:

Automatically Removing the Container

sudo docker container run -i -t –rm –name container1 ubuntu bash

As shown below, it is showing that there is a container running in the system, named as “container1” and it is up from last 3 minutes.

list docker containers

Question: Is it going to self delete on performing “exit” of “log out“?

Answer: Check the following photos for better understanding.

As we have executed the “exit” command for logging out of the container.

log out from docker container

Now, it is time to check the current system state, from the image below it showing 0 running or stopped containers in the system, which means that the “container1” is itself removed after performing “exit” or “log out“.

list running and stopped containers

Conclusion

In this post, we have understood how automatically removing the container helps, moreover what are some commands to exit from the container.

Comment here