| | |

Running Ephemeral Docker Containers – Automatically Remove a Docker Container After Running It

Docker is a convenient tool to quick create containers from different Linux images. If we use the common way to start a docker container like docker run image bash, after the the process exists, the container is still stored there. That is, the docker container persists after it has exited. Sometimes, we would like to do quick tests and tries in some Linux environments in the docker images. And after the program exits, we would like to have the exited container automatically removed.

Start a docker container that will be automatically removed after being stopped

Docker has the support for this kinds of usage. The key option is to use the --rm option for the docker run command.

  --rm true|false
   Automatically remove the container when it exits. The default is false.
  --rm flag can work together with -d, and auto-removal will be done on 
   daemon side.
   Note that it's incompatible with any restart policy other than none.

To start a docker container (from ubuntu:18.04, run bash) that will be automatically removed (deleted):

$ docker run -it --rm ubuntu:18.04 bash

Verify the container will be automatically removed

After we run that, a new container will be created and the bash shell is ready:

root@e18acbc47561:/# 

Now the container can be listed by docker ps:

$ docker ps
CONTAINER ID   IMAGE          COMMAND   CREATED         STATUS         PORTS     NAMES
e18acbc47561   ubuntu:18.04   "bash"    7 minutes ago   Up 7 minutes             priceless_matsumoto

Now let’s exit the cotainer by running exit in the container:

root@e18acbc47561:/# exit
exit

Then, let’s verify the container is really removed using docker ps -a so that all stopped containers will be shown too:

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

The container is gone as expected.

Similar Posts

  • Fedora 中文字体设置

    Fedora 一直有中文字体难看的问题, 尤其是在英文环境中. 使用本文中的配置方法可以得到令人满意的中文效果. 此方案中使用字体都为开源且在Fedora源中自带. 此方案对 Fedora 9 – 20 有效. 对于后续版本支持我会确认并更新此文章. 此方案对Gnome, KDE都有效. Firefox 中也有中文难看的问题, 后面会提到. 快速配置方法 如果你想马上配置好,请使用如下命令。此方法测试使用效果良好。 # yum install cjkuni-ukai-fonts cjkuni-uming-fonts # wget https://raw.githubusercontent.com/zma/config_files/master/others/local.conf \ -O /etc/fonts/local.conf 相关英文字体配置可以参考:Improving Fedora Font Rendering with Open Software and Fonts Only. Fedora 系统中文字体的配置方案 使用uming和ukai字体,即AR PL UMing CN等. 中文字体和等宽字体效果如图所示(点击看大图, Firefox 中文字体设置在后面会提到). 方法如下: 安装字体 首先安装这两个字体: cjkuni-ukai-fonts cjkuni-uming-fonts (在Fedora…

Leave a Reply

Your email address will not be published. Required fields are marked *