Skip to main content

Monofy: Multi-Process Docker Containers

This Python script is designed to manage multiple processes within a single Docker container, ensuring they are started, monitored, and terminated together.

This approach is useful when the processes are tightly integrated and need to operate in unison.

TL;DR

In your Docker file, include:

RUN pip install monofy

CMD ["monofy", "child-command-1", "--param-for-1", "|||", "child-command-2", "--param-for-2"]

Rationale

Running multiple processes in a single Docker container is often discouraged. However, when processes are closely connected and need to work together, this approach can simplify deployment and maintenance.

This script provides a Python-based solution to manage such processes efficiently within a single container, without adding unnecessary complexity. Read more about why this is a great idea

Features

  • Unified Process Management: Starts and manages multiple processes as children of a single parent process.

  • Connected fates: If one child process exits, terminate all others.

  • Signal Handling: Forwards relevant signals (e.g., SIGINT, SIGTERM) to all child processes.

  • Unified Output: Consolidates the output (stdout, stderr) of all child processes.

  • Graceful Shutdown: Ensure the parent process waits for all children to exit before shutting down.

  • Variable Substition: Substitute variables from the command line because Docker can't be bothered to do so itself

Installation

To use this script, clone the repository or install via your preferred method:

pip install monofy

Usage

You can use this script to start and manage multiple processes in a Docker container.

The commands for the processes and their own arguments should be passed as arguments should be passed as arguments to monofy, separated by the literal string |||.

You can also provide "pre commands", one or more commands to run before the parallel processes are started, usually to perform setup tasks like checking the environment. Such pre-commands should be separated by &&.

Examples

Launch two commands in parallel, with connected fates (death of one kills the other):

monofy child-command-1 --param-for-1 '|||' child-command-2 --param-for-2

(Note the single quotes around the operators, which ensure they are not parsed by your shell, but are instead passed on to monofy)

Launch some "pre commands" before launching two commands in parallel:

monofy pre-command-1 --foo=bar '&&' child-command-1 param-for-that '|||' child-command-2 -v

Example Dockerfile

Here’s how you might use this script in a Dockerfile:

FROM [..]

RUN pip install monofy

CMD ["monofy", "check-install", "--deploy", "&&", "web-server", "0.0.0.0:8000", "|||", "background-process", "-v"]

It's possible to pass-in a different combination of commands to start on the invocation of docker run:

docker run -it my-image monofy check-install --deploy '&&' web-server 0.0.0.0:1234 '|||' background-process -vvvv

Usage outside Docker

This package was written with (the limitations of) Docker in mind, but it can also be used in other contexts. One thing I found useful is to use it to start multiple development-related processes in a single line. The property that the death of one process takes down the others is very useful in reducing confusion if only a part of your set of tools goes down.

Alternatives

Use a process manager, like:

However, these tools are often more complex and require additional configuration. They also come with the subtle drawback that it is not possible to change the command to execute when running docker run (because they read config files to determine which commands to start, rather than taking that info from the command-line).

Metadata

Release files for monofy 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monofy 1.2.0
File Size Uploaded Provenance
monofy-1.2.0.tar.gz 8.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for monofy 1.2.0
File Interpreter ABI Platform
monofy-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.3 kB

Release files / monofy-1.2.0.tar.gz

Download URL monofy-1.2.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
98bff64a1be5c4ba0859ea505d3c98f136e3e4479f94c5d47702882f6817d0c3
BLAKE2b-256 checksum
How to use checksums
67bc3f73395eed1cbd865f78bab31635dc9d05a481598353f7950794c467b3d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / monofy-1.2.0-py3-none-any.whl

Download URL monofy-1.2.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da304804591ef0577387b831c9ac045640815291c05bd8c674de23c9f8731687
BLAKE2b-256 checksum
How to use checksums
8f2a2aa32bf43005064aa0f658bb49a9e1d67c35a85736c60d3fd308017f2db6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for monofy 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page