by Adam Brett

Docker Patterns - Transient Container

A Transient Container is the simplest of all of the patterns. It's so simple, in fact, that I almost didn't document it here, but it underpins many other patterns, such as the [Tool Entrypoint] pattern, so that made me think it worthwhile.

When we talk about a Transient Container, we mean a container that runs, does its job, and goes away. This is in contrast to the "normal" method for running a container which starts a daemon either in the foreground or background, and runs until you stop it:

docker run -d -P nginx # background
docker run -P apache # foreground

Instead a Transient Container goes away after it's run, and drops you back to where you were. This is usually controlled by the application you run inside the container, rather than the container itself:

docker run alpine ls

When you use a transient container, you will always omit the -d flag, as you never want to daemonise it in the background. You almost always want the output of the command in your normal terminal, via stdout.

$ docker run alpine ls
bin
dev
etc
home
lib
media
mnt
proc
root
run
sbin
srv
sys
tmp
usr
var

Sometimes, you need to pass arguments to the command you want to run in the container, and that should work too:

$ docker run alpine cat /etc/passwd
root:x:0:0:root:/root:/bin/ash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/usr/lib/news:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucppublic:/sbin/nologin
operator:x:11:0:operator:/root:/bin/sh
man:x:13:15:man:/usr/man:/sbin/nologin
postmaster:x:14:12:postmaster:/var/spool/mail:/sbin/nologin
cron:x:16:16:cron:/var/spool/cron:/sbin/nologin
ftp:x:21:21::/var/lib/ftp:/sbin/nologin
sshd:x:22:22:sshd:/dev/null:/sbin/nologin
at:x:25:25:at:/var/spool/cron/atjobs:/sbin/nologin
squid:x:31:31:Squid:/var/cache/squid:/sbin/nologin
xfs:x:33:33:X Font Server:/etc/X11/fs:/sbin/nologin
games:x:35:35:games:/usr/games:/sbin/nologin
postgres:x:70:70::/var/lib/postgresql:/bin/sh
cyrus:x:85:12::/usr/cyrus:/sbin/nologin
vpopmail:x:89:89::/var/vpopmail:/sbin/nologin
ntp:x:123:123:NTP:/var/empty:/sbin/nologin
smmsp:x:209:209:smmsp:/var/spool/mqueue:/sbin/nologin
guest:x:405:100:guest:/dev/null:/sbin/nologin
nobody:x:65534:65534:nobody:/:/sbin/nologin

If you use this pattern a lot this can potentially lead to a large number of Exited containers left on your system:

9450caedfae7        alpine                                                   "ls"                     24 seconds ago      Exited (0) 24 seconds ago                                       laughing_banach

To avoid this, you should run your Transient Containers with the --rm flag, so that docker removes them as soon as the task is complete. You should only avoid using the --rm flag when you know in advance you will want to inspect the contents of the container later, after the job has completed:

docker run --rm alpine ls

For exclusive content, including screen-casts, videos, and early beta access to my projects, subscribe to my email list below.


I love discussion, but not blog comments. If you want to comment on what's written above, head over to twitter.