Docker Community Forums

Share and learn in the Docker community.

Exiting a linux docker container


(Lnelson) #1

Hi, I would like to know how to have a linux (RHEL/CENTOS/SL 6.8) container exit immediately
after running a test script at init level 3 (after networking starts) something on the order of
(wouldn’t it be nice if there was a language to create such a script from human language!)

[note variables are text, not numeric]

$FOO=[some string determined at startup]
$BAR=[another string determined at startup]
If $FOO .ne $BAR then exit this container with message:
“Sorry, this container will not run on your system.”

I have tried a number of /bin/bash scripts, none of which seem to work.
The container does not like “logout” nor “shutdown -h now” nor “exit” nor
"kill -9 $PPID".
It only seems to accept “exit” from the command line.

Any suggestions?
Thanks!

  • Larry

(David Maze) #2

Most containers don’t run an init system and don’t “start networking”. (And, for that matter, “init level 3” no longer exists on almost any Linux distribution, with CentOS 6 AFAIK being almost the last thing surviving to use a System V style init.)

#!/bin/sh
if [ -z "$PGHOST" ]; then
  echo "You must configure \$PGHOST so this container can contact the database." >&2
  exit 1
fi
exec "$@"

Make a script like this the ENTRYPOINT of your container, and it will refuse to do anything (even start a shell for debugging purposes) if the PGHOST environment variable isn’t set. If it is, then it will do whatever the container’s CMD is or whatever got passed on the docker run command line.