Skip to content

Tiny rc script for multi-process Linux containers.

License

Notifications You must be signed in to change notification settings

madogiwa/tiny-rc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is "rc" script for multi-process Linux containers implement by shell script only.

What is useful for this?

Sometimes, to make a multi-process container is inevitable. A simple approach to make multi-process container is write shell script as following.

#!/bin/sh
cron                    # run cron as daemon
nginx -g 'daemon off;'  # run nginx as foreground process

## In Dockerfile, specify this shell script as CMD
## CMD ["run.sh"]

This approach has disadvantage which do not have error handling. For example, even if cron dies, nginx will keep running.

An other approach is introduce a supervisor program such as supervisord. Supervisor program has error handling. However, this approach has another disadvantage which is painfull of install. How to install of supervisor is depend on base image as follows.

## for debian
RUN apt-get update && apt-get install -y supervisor

## for alpine
RUN apk update && apk add -u py-pip && pip install supervisor

## for centos:7
RUN yum install -y epel-release && yum install -y python-pip && pip install superisor

## for ...
RUN ...

The tiny-rc is implemented by shell script only. So no additional install needed. Also, the tiny-rc is supported various containers such as:

How to Use

The example Dockerfile is as follows.

##
## init (for download only)
##
FROM alpine AS init

RUN wget -O /tini https://github.com/krallin/tini/releases/download/v0.18.0/tini-static-amd64 && \
    chmod +x /tini

RUN wget -O /tiny-rc https://github.com/madogiwa/tiny-rc/releases/download/v0.1.7/tiny-rc && \
    chmod +x /tiny-rc


##
## main
##
FROM debian

# The tiny-rc cannot use as PID 1.
# You need specifiy any init program into ENTRYPOINT.
COPY --from=init /tini /tini
COPY --from=init /tiny-rc /tiny-rc
ENTRYPOINT ["/tini", "--", "/tiny-rc"]

# (*.unit|*.service) store into tiny-rc.d if needed
COPY tiny-rc.d /tiny-rc.d

# Your main service is specified into CMD.
COPY main_service /app/main_service 
CMD ["/app/main_service"]

The tiny-rc performs following steps.

  1. execute *.unit in $TINYRC_INIT_DIR.
  2. start *.service in $TINYRC_INIT_DIR as background process.
  3. execute CMD.
  4. wait until any *.service or CMD exited. ('liveness probe')
  5. send $TINYRC_SHUTDOWN_SIGNAL to all *.service and CMD.
  6. wait until all *.service and CMD exited. ('shutdown probe')
  7. exit with CMD's exit code.
  • Default $TINYRC_INIT_DIR is /tiny-rc.d.
  • Defalut $TINYRC_SHUTDOWN_SIGNAL is TERM.

Environments

You can customize behavior using environment value.

name default value description
TINYRC_INIT_DIR /tiny-rc.d directory path
TINYRC_LIVENESS_PROBE_INTERVAL 1 interval of 'liveness probe'
TINYRC_SHUTDOWN_PROBE_INTERVAL 1 interval of 'shutdown probe'
TINYRC_SHUTDOWN_TIMEOUT 90 send SIGTERM to PID 1 and exit tiny-rc when 'shutdown probe' timeout exceed.
TINYRC_SHUTDOWN_SIGNAL TERM signal type which is sent after 'liveness probe' finished.
TINYRC_DISABLE_LOGGING (undef) disable logging
TINYRC_LOG_LEVEL 5 log level (3=ERROR, 5=WARN, 7=INFO, 9=DEBUG)