GithubHelp home page GithubHelp logo

ahmetb / multi-process-container Goto Github PK

View Code? Open in Web Editor NEW
29.0 3.0 5.0 8 KB

Example docker container image with multiple services supervised by s6 init process

License: Apache License 2.0

Dockerfile 46.05% Shell 53.95%

multi-process-container's Introduction

Sample multi-process container with s6-overlay

Normally, you should run one process per container for many good reasons. However, sometimes you need two services running next to each other.

This repository is a sample Docker image that uses s6-overlay that is s6 optimized for containers as the init process as the container’s ENTRYPOINT.

Once the container started, it will run /init as its ENTRYPOINT, which will scan /etc/services.d/for sub-directories defining the services like this:

/etc
└── services.d/
    ├── service1/
    │   ├── run     # starts a python web server
    │   └── finish  # runs when service1 terminates
    └── service2/
        ├── run     # starts command "sleep 99999999"
        └── finish  # runs when service2 terminates
  • run file is a small bash script that starts the service
  • finish file runs when the service terminates. This is where you decide whether the container should terminate when the service dies, or should s6 restart the process.

Review: example run script

This script starts your service in the foreground (not like a daemon):

#!/usr/bin/with-contenv sh
echo >&2 "starting service1"
exec python -m http.server "${PORT:-8080}"

Note we used #!/usr/bin/with-contenv sh (or ... bash). This helps us inherit environment variables of the container.

If you don't do that and do your usual #!/bin/bash (or even #!/usr/bin/env bash) etc, you will get fewer environment variables. (You can also run the container with S6_KEEP_ENV=1 environment variable to avoid using with-contenv.)

The exec ... command starts the long-running service process in the foreground and most importantly, exec replaces the current shell process so that our service is directly managed by s6-supervise (without sh/bash as its parent process).

Review: example finish script

This command gets the exit code of the run script as $1 argument. When the service terminates, you can do either of these things

  • Terminate the container and kill the other service: see example below.
  • Have the service restarted by s6: either delete the finish script or don't use any commands like below that terminates the init process.
#!/usr/bin/env sh
echo >&2 "service1 exited. code=${1}"

# terminate other services to exit from the container
exec s6-svscanctl -t /var/run/s6/services

Try it out

docker build -t test-image .
docker run --rm -i -t --name=test test-image

On another terminal, run docker exec test pstree which will show the process tree inside the container with service1 (python) and service2 (sleep):

s6-svscan-+-s6-supervise
          |-s6-supervise---python
          `-s6-supervise---sleep

Similarly, you can send a graceful termination signal SIGTERM to the container and have both services terminate and their finish script runs:

docker kill --signal=TERM test
service1 exited. code=256
service2 exited. code=256
[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] sending all processes the TERM signal.
[s6-finish] sending all processes the KILL signal and exiting.

Learn more

Hope you can use this sample to build containers that reliably run multiple processes in production. Remember, you should avoid doing this and split your processes into multiple containers as much as possible.

You can learn more at s6-overlay repository which has a great user manual. The s6 project page goes into more technical details.

multi-process-container's People

Contributors

ahmetb avatar

Stargazers

Philip avatar Magno Oliveira avatar  avatar jrccrj avatar Hasan Tayyar Beşik avatar Abdulmelik Kalkan avatar  avatar David Ellis avatar Walter C avatar jian avatar salah731 avatar Denisov Mikhail avatar Sami Hoda avatar Subodh Pachghare avatar mkqi avatar  avatar Manuel GP avatar James Veitch avatar  avatar Sebastian Nowak avatar Mo Firouz avatar Tasdik Rahman avatar Lei Tu avatar Brandon B. Jozsa avatar Cedric Kring avatar Ziλ∀ avatar Devin Pastoor avatar Ankur Srivastava avatar Owais Lone avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.