Container startup system / maintenance and cleaning

November 12, 2021 - Reading time: 2 minutes

Docker can be instructed to automatically restart a failing container (failing in this context means that the last process in the container finished / exited)

test@localhost: sudo docker run -d --restart 
always          no              on-failure      on-failure:     unless-stopped 

always will restart every time the container exits - with an exponential back-off no default on-failure when it exits with non-zero status with optional :max-retries unless-stopped don't do it on containers that are stopped.

A more elegant way - particularly when dealing with multiple processes inside a container - to control the container is to use an image that includes a sys init. (tini, supervisord, runinit)

test@localhost: sudo docker run -d -p 80:80 --name lamp tutum/lamp
test@localhost: sudo docker exec lamp ps
    PID TTY          TIME CMD
      1 ?        00:00:00 supervisord
    434 ?        00:00:00 mysqld_safe
    435 ?        00:00:00 apache2
    816 ?        00:00:00 ps
test@localhost: sudo docker top lamp
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                12215               12193               0                   17:30               ?                   00:00:00            /usr/bin/python /usr/bin/supervisord -n
root                12675               12215               0                   17:30               ?                   00:00:00            /bin/sh /usr/bin/mysqld_safe
root                12676               12215               0                   17:30               ?                   00:00:00            apache2 -D FOREGROUND
www-data            12774               12676               0                   17:30               ?                   00:00:00            apache2 -D FOREGROUND
www-data            12776               12676               0                   17:30               ?                   00:00:00            apache2 -D FOREGROUND
www-data            12779               12676               0                   17:30               ?                   00:00:00            apache2 -D FOREGROUND
www-data            12780               12676               0                   17:30               ?                   00:00:00            apache2 -D FOREGROUND
www-data            12781               12676               0                   17:30               ?                   00:00:00            apache2 -D FOREGROUND
systemd+            13039               12675               0                   17:30               ?                   00:00:00            /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306

Entry point read by docker before it runs specific container commands - in this example it was a bash script, that I replaced with "cat" and then passed a parameter (the file name)

test@localhost: sudo docker run --entrypoint="cat" wordpress:php8.0-apache /usr/local/bin/docker-entrypoint.sh
#!/usr/bin/env bash
set -Eeuo pipefail

if [[ "$1" == apache2* ]] || [ "$1" = 'php-fpm' ]; then
    uid="$(id -u)"
    gid="$(id -g)"
    if [ "$uid" = '0' ]; then