# crazy-max/docker-msmtpd architecture

> Architecture diagram of the GitHub repository crazy-max/docker-msmtpd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/crazy-max/docker-msmtpd
- Repository: https://github.com/crazy-max/docker-msmtpd

## Overview

1. **What this repo is**
   - This repository builds and ships a **container image for `msmtpd`**, a lightweight SMTP relay.
   - It is not an application with a server/UI; the core product is the **runtime Docker image** published to Docker Hub and GHCR.

2. **Primary runtime boundary**
   - The container exposes a **single SMTP relay port: `2500`**.
   - Incoming mail is accepted by `msmtpd` inside the container and then forwarded to an **upstream SMTP server** configured by environment variables.

3. **Core data flow**
   - External clients connect to port `2500` on the container.
   - `msmtpd` reads runtime configuration derived from environment variables such as `SMTP_HOST`, `SMTP_PORT`, `SMTP_TLS`, `SMTP_AUTH`, and related message/header options.
   - The relay then authenticates and forwards mail to the configured upstream SMTP host.

4. **Configuration model**
   - The container is configured almost entirely through **environment variables** rather than files baked into the image.
   - Sensitive values can also be provided via `*_FILE` variants for Docker secrets, specifically for `SMTP_USER` and `SMTP_PASSWORD`.
   - Runtime options cover transport security, auth, EHLO domain, envelope-from behavior, header rewriting, and DSN behavior.

5. **Container bootstrap / init layer**
   - The `rootfs/etc/cont-init.d/` directory contains container startup scripts that prepare the runtime environment before the service starts.
   - Based on the filenames, these scripts handle:
     - log directory fixes,
     - UID/GID adjustment for non-root execution,
     - config generation or normalization,
     - service definition creation.
   - This suggests the image uses an init system pattern where bootstrapping is separated from the long-running SMTP process.

6. **Non-root execution and identity management**
   - A key architectural constraint is that the daemon runs as a **non-root user**.
   - `PUID` and `PGID` are used to align the container process with host/user expectations, which affects file ownership and permissions inside mounted volumes or logs.

7. **Build and packaging**
   - The image is built from source with **Docker Buildx** and `docker-bake.hcl`.
   - The repo is set up for **multi-platform builds**, covering multiple Linux architectures.
   - The `Dockerfile` and root filesystem layout indicate a packaging model where a base image is assembled with the compiled/bundled `msmtpd` runtime and init scripts.

8. **Distribution targets**
   - The image is published to:
     - **Docker Hub** as `crazymax/msmtpd`
     - **GitHub Container Registry** as `ghcr.io/crazy-max/msmtpd`
   - The README and workflows show this repo is meant to produce a reproducible container artifact rather than a library or service component.

9. **Deployment examples**
   - `examples/compose/compose.yml` demonstrates the expected Docker Compose deployment pattern.
   - `examples/kubernetes/deployment.yaml` shows how the same container is deployed in Kubernetes.
   - These examples are important because they document the minimal runtime wiring: image, port exposure, and environment-based SMTP configuration.

10. **Testing and validation**
   - The `test/compose.yml` file suggests validation is done through containerized integration testing, likely exercising startup and config behavior in a Compose environment.
   - GitHub Actions workflows (`build.yml`, `test.yml`, `labels.yml`, `zizmor.yml`) handle build, test, and repository hygiene checks.

11. **Operational dependencies**
   - The only external service the runtime fundamentally depends on is the **upstream SMTP relay server** specified by `SMTP_HOST`.
   - Optional external integrations are the container registries used for distribution and the notification/build automation in GitHub Actions.

12. **Diagram boundaries to capture**
   - A useful architecture diagram should show:
     - **Client SMTP producers** → container port `2500`
     - **Container init scripts** → configure user, logs, and service startup
     - **`msmtpd` relay process** → applies env-based policy and forwards mail
     - **Upstream SMTP server** → receives relayed messages
     - Separate lanes for **build/publish pipeline** and **example deployment manifests**.

Main entry points: [`Dockerfile`](https://github.com/crazy-max/docker-msmtpd/tree/master/Dockerfile) (Dockerfile), [`.github/workflows/build.yml`](https://github.com/crazy-max/docker-msmtpd/blob/master/.github/workflows/build.yml) (Build workflow), [`examples/compose/compose.yml`](https://github.com/crazy-max/docker-msmtpd/blob/master/examples/compose/compose.yml) (Compose example), [`rootfs/etc/cont-init.d/00-fix-logs.sh`](https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/00-fix-logs.sh) (Fix logs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime image"]
  node_smtp_clients(("SMTP clients<br/>external producer"))
  node_port_2500["Port 2500<br/>exposed port"]
  node_env_config["Env config<br/>runtime config"]
  node_secret_files["Secret files<br/>secret inputs"]
  node_msmtpd{{"msmtpd<br/>smtp relay"}}
  node_upstream_smtp[("Upstream SMTP<br/>external server")]
end

subgraph group_bootstrap["Bootstrap"]
  node_fix_logs["Fix logs<br/>init script<br/>[00-fix-logs.sh]"]
  node_fix_uidgid["Fix uid/gid<br/>init script<br/>[01-fix-uidgid.sh]"]
  node_config_init["Config init<br/>init script<br/>[02-config.sh]"]
  node_service_init["Service init<br/>init script"]
end

subgraph group_delivery["Build and publish"]
  node_dockerfile["Dockerfile<br/>image recipe"]
  node_bakefile["Buildx bake<br/>build plan<br/>[docker-bake.hcl]"]
  node_build_workflow["Build workflow<br/>ci pipeline<br/>[build.yml]"]
  node_test_workflow["Test workflow<br/>ci pipeline<br/>[test.yml]"]
  node_dockerhub[("Docker Hub<br/>registry target")]
  node_ghcr[("GHCR<br/>registry target")]
end

subgraph group_examples["Examples and tests"]
  node_compose_example["Compose example<br/>deployment example<br/>[compose.yml]"]
  node_k8s_example["K8s example<br/>deployment example<br/>[deployment.yaml]"]
  node_test_compose["Test compose<br/>integration fixture<br/>[compose.yml]"]
end

node_smtp_clients -->|"connect to"| node_port_2500
node_port_2500 -->|"accepts"| node_msmtpd
node_env_config -->|"configures"| node_msmtpd
node_secret_files -->|"secrets for"| node_msmtpd
node_msmtpd -->|"forwards to"| node_upstream_smtp
node_fix_logs -->|"prepares"| node_msmtpd
node_fix_uidgid -->|"sets identity"| node_msmtpd
node_config_init -->|"materializes"| node_env_config
node_service_init -->|"starts"| node_msmtpd
node_dockerfile -->|"packages"| node_msmtpd
node_dockerfile -->|"includes"| node_fix_logs
node_dockerfile -->|"includes"| node_fix_uidgid
node_dockerfile -->|"includes"| node_config_init
node_dockerfile -->|"includes"| node_service_init
node_bakefile -->|"builds"| node_dockerfile
node_build_workflow -->|"drives"| node_bakefile
node_build_workflow -->|"publishes"| node_dockerhub
node_build_workflow -->|"publishes"| node_ghcr
node_compose_example -->|"runs"| node_msmtpd
node_compose_example -->|"sets"| node_env_config
node_k8s_example -->|"deploys"| node_msmtpd
node_test_compose -->|"validates"| node_msmtpd
node_test_workflow -->|"runs"| node_test_compose

click node_fix_logs "https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/00-fix-logs.sh"
click node_fix_uidgid "https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/01-fix-uidgid.sh"
click node_config_init "https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/02-config.sh"
click node_service_init "https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/03-create-service.sh"
click node_dockerfile "https://github.com/crazy-max/docker-msmtpd/tree/master/Dockerfile"
click node_bakefile "https://github.com/crazy-max/docker-msmtpd/blob/master/docker-bake.hcl"
click node_build_workflow "https://github.com/crazy-max/docker-msmtpd/blob/master/.github/workflows/build.yml"
click node_test_workflow "https://github.com/crazy-max/docker-msmtpd/blob/master/.github/workflows/test.yml"
click node_compose_example "https://github.com/crazy-max/docker-msmtpd/blob/master/examples/compose/compose.yml"
click node_k8s_example "https://github.com/crazy-max/docker-msmtpd/blob/master/examples/kubernetes/deployment.yaml"
click node_test_compose "https://github.com/crazy-max/docker-msmtpd/blob/master/test/compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_smtp_clients,node_port_2500,node_env_config,node_secret_files,node_msmtpd,node_upstream_smtp toneBlue
class node_fix_logs,node_fix_uidgid,node_config_init,node_service_init toneAmber
class node_dockerfile,node_bakefile,node_build_workflow,node_test_workflow,node_dockerhub,node_ghcr toneMint
class node_compose_example,node_k8s_example,node_test_compose toneRose
```

## Components

### Runtime image

Containerized msmtpd relay and its startup configuration

- **SMTP clients**: external producer · Mail senders connecting to the relay
- **Port 2500**: exposed port · Container SMTP relay entrypoint
- **Env config**: runtime config · SMTP_HOST, SMTP_PORT, TLS, auth, headers, DSN, and related behavior
- **Secret files**: secret inputs · *_FILE variants for SMTP user and password
- **msmtpd**: smtp relay · Accepts mail and forwards it to an upstream SMTP server
- **Upstream SMTP**: external server · Destination relay server configured by SMTP_HOST

### Bootstrap

Init scripts and service wiring inside the image

- [Fix logs](https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/00-fix-logs.sh): init script · Prepares log directory ownership and access · `rootfs/etc/cont-init.d/00-fix-logs.sh`
- [Fix uid/gid](https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/01-fix-uidgid.sh): init script · Aligns container identity with PUID and PGID · `rootfs/etc/cont-init.d/01-fix-uidgid.sh`
- [Config init](https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/02-config.sh): init script · Generates or normalizes runtime configuration · `rootfs/etc/cont-init.d/02-config.sh`
- [Service init](https://github.com/crazy-max/docker-msmtpd/blob/master/rootfs/etc/cont-init.d/03-create-service.sh): init script · Creates the service definition for startup · `rootfs/etc/cont-init.d/03-create-service.sh`

### Build and publish

Image build, multi-platform packaging, and registry distribution

- [Dockerfile](https://github.com/crazy-max/docker-msmtpd/tree/master/Dockerfile): image recipe · Assembles the runtime image and bundled msmtpd components · `Dockerfile`
- [Buildx bake](https://github.com/crazy-max/docker-msmtpd/blob/master/docker-bake.hcl): build plan · Multi-platform build orchestration · `docker-bake.hcl`
- [Build workflow](https://github.com/crazy-max/docker-msmtpd/blob/master/.github/workflows/build.yml): ci pipeline · Build and publish automation in GitHub Actions · `.github/workflows/build.yml`
- [Test workflow](https://github.com/crazy-max/docker-msmtpd/blob/master/.github/workflows/test.yml): ci pipeline · Validation workflow for the container image and config behavior · `.github/workflows/test.yml`
- **Docker Hub**: registry target · Published image destination cra ymax/msmtpd
- **GHCR**: registry target · Published image destination ghcr.io/crazy-max/msmtpd

### Examples and tests

Deployment manifests and container-based validation

- [Compose example](https://github.com/crazy-max/docker-msmtpd/blob/master/examples/compose/compose.yml): deployment example · Docker Compose wiring for the relay container · `examples/compose/compose.yml`
- [K8s example](https://github.com/crazy-max/docker-msmtpd/blob/master/examples/kubernetes/deployment.yaml): deployment example · Kubernetes deployment manifest for the image · `examples/kubernetes/deployment.yaml`
- [Test compose](https://github.com/crazy-max/docker-msmtpd/blob/master/test/compose.yml): integration fixture · Compose-based runtime validation setup · `test/compose.yml`

## Connections

- SMTP clients → Port 2500: connect to · External senders reach the exposed SMTP port
- Port 2500 → msmtpd: accepts · The container port feeds the relay process
- Env config → msmtpd: configures · Runtime behavior is driven by environment variables
- Secret files → msmtpd: secrets for · Credentials may be provided through file-backed secrets
- msmtpd → Upstream SMTP: forwards to · Accepted mail is relayed to the configured upstream server
- Fix logs → msmtpd: prepares · Ensures log paths are usable before the service starts
- Fix uid/gid → msmtpd: sets identity · Adjusts permissions for non-root execution
- Config init → Env config: materializes · Normalizes environment-derived settings into runtime config
- Service init → msmtpd: starts · Defines the long-running relay service
- Dockerfile → msmtpd: packages · Build recipe assembles the runtime image around the relay
- Dockerfile → Fix logs: includes · Image embeds init scripts in the root filesystem
- Dockerfile → Fix uid/gid: includes · Image embeds init scripts in the root filesystem
- Dockerfile → Config init: includes · Image embeds init scripts in the root filesystem
- Dockerfile → Service init: includes · Image embeds init scripts in the root filesystem
- Buildx bake → Dockerfile: builds · Buildx bake orchestrates image builds
- Build workflow → Buildx bake: drives · CI uses the build plan for multi-platform publishing
- Build workflow → Docker Hub: publishes · Build output is pushed to Docker Hub
- Build workflow → GHCR: publishes · Build output is pushed to GitHub Container Registry
- Compose example → msmtpd: runs · Demonstrates the expected container runtime wiring
- Compose example → Env config: sets · Shows the required environment-based SMTP configuration
- K8s example → msmtpd: deploys · Shows Kubernetes deployment of the same image
- Test compose → msmtpd: validates · Containerized integration testing exercises startup and config behavior
- Test workflow → Test compose: runs · CI executes the Compose-based validation fixture
