{"id":4024,"date":"2022-07-06T12:48:37","date_gmt":"2022-07-06T11:48:37","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=4024"},"modified":"2022-07-06T12:48:37","modified_gmt":"2022-07-06T11:48:37","slug":"how-to-automatically-delete-a-docker-container-after-running-it","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2022\/07\/06\/how-to-automatically-delete-a-docker-container-after-running-it\/","title":{"rendered":"How to automatically delete a Docker container after running it"},"content":{"rendered":"<p>Normally, a Docker container persists after it has exited. This allows you to run the container again, inspect its filesystem, and so on. However, sometimes you want to run a container and delete it immediately after it exits. Docker provides the &#8211;rm command-line option for this purpose:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">docker run --rm &lt;name_of_docker_image&gt;<\/pre>\n<p>This command will create a running container from the &#8220;name_of_docker_image&#8221; image and delete the container immediately after we stop it. This helps to prevent having to clean up containers after you&#8217;re done your experiment.<\/p>\n<p><strong>Note:<\/strong>\u00a0 The <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">--rm<\/code> flag is required to archive this purpose doesn&#8217;t work in conjunction with the -d (&#8211;detach) flag. When the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">--rm<\/code> flag is set, Docker also removes the volumes associated with the container when the container is removed. This is similar to running <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">docker rm -v my-container<\/code>. Only volumes that are specified without a name are removed.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">docker run -it --rm -v \/etc -v logs:\/var\/log centos \/usr\/local\/bin\/tomcat\/logs<\/pre>\n<p>With the command above, the volume of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/etc<\/code> will be removed, but the volume of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/var\/log<\/code> will not. Volumes inherited via <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">--volumes-from<\/code> will be removed with the same logic &#8212; if the original volume was specified with a name it will not be removed.<\/p>\n<p>That&#8217;s it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Normally, a Docker container persists after it has exited. This allows you to run the container again, inspect its filesystem, and so on. However, sometimes you want to run a&hellip; <\/p>\n","protected":false},"author":2,"featured_media":4041,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[730,722,724,174],"tags":[745,737],"class_list":["post-4024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","category-devops","category-docker","category-linux","tag-docker","tag-docker-recipes"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/4024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=4024"}],"version-history":[{"count":1,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/4024\/revisions"}],"predecessor-version":[{"id":4025,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/4024\/revisions\/4025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/4041"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=4024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=4024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=4024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}