Setting up Docker instances with a init

Introduction

We’ve found a few methods to have a Docker instance look-alike a system container

  1. run dumb-init and rely on the cron daemon as an instance-holder
  2. run dumb-init and bash/wait for all daemons to finish? // TODO
  3. run Devuan’s SVR4 init which survives just fine within an instance
  4. run sinit instead? // TODO
  5. enable SYS_ADMIN and simply run systemd within

Dumb-init & cron

It works for Ubuntu], CentOS and Devuan! Devuan is preferred, though, as the init scripts within will just work out-of-the-box.

Make sure either dumb-init or tini get installed within the image.

Prepare a specially crafted rc.local within the image e.g.

#!/bin/bash

# daemon1 goes to background...
# daemon2 goes to background...

# last daemon goes to foreground
echo starting cron daemon as the instance-holder
exec /usr/sbin/cron -f

and within the image itself

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/usr/bin/bash", "-c", "/etc/rc.local"]

the tini alternative to dumb-init should also work

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/usr/bin/bash", "-c", "/etc/rc.local"]

one shot – this is somehow the equivalent of what the built-in tini would do

untested

docker run -d --init pbraun9/ubuntu "bash -c /etc/rc.local"

Dumb-init & bash/wait

untested

Prepare a specially crafted rc.local within the image e.g.

#!/bin/bash

echo -n cron...
/usr/sbin/cron && echo done || echo FAIL

wait -n

and within the image itself

# not sure that works
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/usr/sbin/cron", "-f"]

Devuan’s SVR4

It works!

SYS_ADMIN/systemd

DRAFT – cannot test a systemd instance on slackware

Within the image itself

...

Create an instance and run systemd within

docker run -dti --name train-shuffle --hostname train-shuffle \
    --cap-add=SYS_ADMIN \
    -e "container=docker" \
    -v /sys/fs/cgroup:/sys/fs/cgroup \
    pbraun9/ubuntu /sbin/init
^P ^Q

Additional notes

That does NOT work in case there’s a sheebang in rc.local, as bash then becomes associated with the init

ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/etc/rc.local"]

Troubles

[WARN  tini (7)] Tini is not running as PID 1 and isn't registered as a child subreaper.
Zombie processes will not be re-parented to Tini, so zombie reaping won't work.
To fix the problem, use the -s option or set the environment variable TINI_SUBREAPER to register Tini as a child subreaper, or run Tini as PID 1.

Resources

Docker How to run /usr/sbin/init and then other scripts on start up [closed] https://stackoverflow.com/questions/48720049/docker-how-to-run-usr-sbin-init-and-then-other-scripts-on-start-up

Startup script after /usr/sbin/init #118 https://github.com/CentOS/sig-cloud-instance-images/issues/118

The centos:centos7 image doesn’t have “service” command #28 https://github.com/CentOS/sig-cloud-instance-images/issues/118

systemd

Running systemd within a Docker Container https://developers.redhat.com/blog/2014/05/05/running-systemd-within-docker-container

RUNNING SYSTEMD WITHIN A DOCKER CONTAINER https://rhatdan.wordpress.com/2014/04/30/running-systemd-within-a-docker-container/

privileged container which is running init process inside a container kills all TTY sessions on host #106 https://github.com/docker/for-linux/issues/106 –> SYS_ADMIN and cgroup

dumb-init

Choosing an init process for multi-process containers https://ahmet.im/blog/minimal-init-process-for-containers/

Introducing dumb-init, an init system for Docker containers https://engineeringblog.yelp.com/2016/01/dumb-init-an-init-for-docker.html

dumb-init https://github.com/Yelp/dumb-init

tini

Tini - A tiny but valid init for containers https://github.com/krallin/tini

Tini Automated Builds https://github.com/krallin/tini-images


GUIDES | LECTURES | BENCHMARKS | SMTP HEALTH