| | |

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

  • How to import OCaml libraries

    How to import 3rd party libraries (e.g. not standard libraries) in OCaml? An answer by Gabriel Scherer on how to import Batteries is just great and answers this question with much information. Although it is for Batteries, the method is general. The OCaml compiler (or toplevel, etc.) will find with no additional information only the…

  • Installing Dropbox on Linux

    How to install Dropbox on Linux ? Just follow Dropbox’s instruction here: https://www.dropbox.com/install?os=lnx A quick command for 64-bit Linux: $ cd ~ && wget -O – “https://www.dropbox.com/download?plat=lnx.x86_64” | tar xzf – Use the dropx CLI: http://www.dropboxwiki.com/Using_Dropbox_CLI Download dropbox.py: $ wget -O ~/bin/dropbox.py “https://www.dropbox.com/download?dl=packages/dropbox.py” Set the permissions: $ chmod +x ~/bin/dropbox.py Start Dropbox: $ dropbox.py start Status:…

  • Formatting code shortcuts in Eclipse

    Formatting code shortcuts in Eclipse. Shortcut: Ctrl + Shift + F No need to select the code. Read more: C++ cout formatting output Large-but-correctly-aligned-and-optimized code is faster than less-bytes-per-instruction/opcode-packed code How to get the assembly code for OCaml code generated by ocamlopt? Google Chrome keyboard and mouse shortcuts for Linux and Windows In Vim, are…

  • mrcc – A Distributed C Compiler System on MapReduce

    The mrcc project’s homepage is here: mrcc project. Abstract mrcc is an open source compilation system that uses MapReduce to distribute C code compilation across the servers of the cloud computing platform. mrcc is built to use Hadoop by default, but it is easy to port it to other could computing platforms, such as MRlite,…

  • How to set up MySQL replication

    How to set up MySQL replication. And for debugging purpose, it will be great if I can run multiple MySQL instances on a single host. To set up MySQL replication, check these tutorials: MySQL Replication MySQL replication setup You can Setup multiple MySQL instances on single server. Read more: How to set the data replication…

Leave a Reply

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