mirror of
https://github.com/chatmail/relay.git
synced 2026-05-15 10:24:40 +00:00
feat: add Docker Compose support
Add container-based deployment as an alternative to bare-metal pyinfra. - systemd inside container reusing the existing deployer infrastructure - chatmail-init.sh runs `cmdeploy run --ssh-host @local` on first boot, so the container self-deploys using the same code path as bare-metal - Config via MAIL_DOMAIN env var (simple) or mounted chatmail.ini (advanced) - External TLS support via TLS_EXTERNAL_CERT_AND_KEY for reverse proxy setups - Image version tracking in /etc/chatmail-image-version for upgrade detection - .git/ excluded, but version file mocked so git revparse still works - Health check verifies postfix, dovecot, and nginx are listening Files added: - docker/chatmail_relay.dockerfile: multi-stage build (build + runtime) - docker/chatmail-init.sh: first-boot deployment script - docker/chatmail-init.service: systemd unit for init script - docker/entrypoint.sh: container entrypoint (starts systemd) - docker/healthcheck.sh: container health check - docker/docker-compose.yaml: main compose config - docker/docker-compose.ci.yaml: CI override (uses GHCR image) - docker/docker-compose.override.yaml.example: customization template - docker/build.sh: helper script - doc/source/docker.rst: documentation - .dockerignore: build context filter
This commit is contained in:
9
docker/build.sh
Executable file
9
docker/build.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
# Build the chatmail Docker image with the current git hash baked in.
|
||||
# Usage: ./docker/build.sh [extra docker-compose build args...]
|
||||
#
|
||||
# .git/ is excluded from the build context (.dockerignore) so the hash
|
||||
# must be passed as a build arg from the host.
|
||||
|
||||
export GIT_HASH=$(git rev-parse HEAD)
|
||||
exec docker compose -f docker/docker-compose.yaml build "$@"
|
||||
14
docker/chatmail-init.service
Normal file
14
docker/chatmail-init.service
Normal file
@@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=Run container setup commands
|
||||
After=multi-user.target
|
||||
ConditionPathExists=/chatmail-init.sh
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/bin/bash /chatmail-init.sh
|
||||
RemainAfterExit=true
|
||||
WorkingDirectory=/opt/chatmail
|
||||
PassEnvironment=<envs_list>
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
87
docker/chatmail-init.sh
Executable file
87
docker/chatmail-init.sh
Executable file
@@ -0,0 +1,87 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
export CHATMAIL_INI="${CHATMAIL_INI:-/etc/chatmail/chatmail.ini}"
|
||||
export CHATMAIL_NOSYSCTL=True
|
||||
export CHATMAIL_NOPORTCHECK=True
|
||||
|
||||
CMDEPLOY=/opt/cmdeploy/bin/cmdeploy
|
||||
|
||||
if [ -z "$MAIL_DOMAIN" ]; then
|
||||
echo "ERROR: Environment variable 'MAIL_DOMAIN' must be set!" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Generate DKIM keys if not mounted
|
||||
if [ ! -f /etc/dkimkeys/opendkim.private ]; then
|
||||
/usr/sbin/opendkim-genkey -D /etc/dkimkeys -d "$MAIL_DOMAIN" -s opendkim
|
||||
fi
|
||||
# Fix ownership for bind-mounted keys (host opendkim UID may differ from container)
|
||||
chown -R opendkim:opendkim /etc/dkimkeys
|
||||
|
||||
# Create chatmail.ini, skip if mounted
|
||||
mkdir -p "$(dirname "$CHATMAIL_INI")"
|
||||
if [ ! -f "$CHATMAIL_INI" ]; then
|
||||
$CMDEPLOY init --config "$CHATMAIL_INI" "$MAIL_DOMAIN"
|
||||
fi
|
||||
|
||||
# Auto-detect IPv6: if the host has no IPv6 connectivity, set disable_ipv6
|
||||
# in the ini so dovecot/postfix/nginx bind to IPv4 only.
|
||||
# Uses network_mode:host so /proc/net/if_inet6 reflects the host's stack.
|
||||
if [ ! -e /proc/net/if_inet6 ]; then
|
||||
if grep -q '^disable_ipv6 = False' "$CHATMAIL_INI"; then
|
||||
sed -i 's/^disable_ipv6 = False/disable_ipv6 = True/' "$CHATMAIL_INI"
|
||||
echo "[INFO] IPv6 not available, set disable_ipv6 = True"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Inject external TLS paths from env var unless defined in chatmail.ini
|
||||
if [ -n "${TLS_EXTERNAL_CERT_AND_KEY:-}" ]; then
|
||||
if ! grep -q '^tls_external_cert_and_key' "$CHATMAIL_INI"; then
|
||||
echo "tls_external_cert_and_key = $TLS_EXTERNAL_CERT_AND_KEY" >> "$CHATMAIL_INI"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Ensure mailboxes directory exists (chatmail-metadata needs it at startup,
|
||||
# but Dovecot only creates it on first mail delivery)
|
||||
mkdir -p "/home/vmail/mail/${MAIL_DOMAIN}"
|
||||
chown vmail:vmail "/home/vmail/mail/${MAIL_DOMAIN}"
|
||||
|
||||
# --- Deploy fingerprint: skip cmdeploy run if nothing changed ---
|
||||
# On restart with identical image+config, systemd already brings up all
|
||||
# enabled services only configure+activate are needed here.
|
||||
IMAGE_VERSION_FILE="/etc/chatmail-image-version"
|
||||
FINGERPRINT_FILE="/etc/chatmail/.deploy-fingerprint"
|
||||
image_ver="none"
|
||||
[ -f "$IMAGE_VERSION_FILE" ] && image_ver=$(cat "$IMAGE_VERSION_FILE")
|
||||
config_hash=$(sha256sum "$CHATMAIL_INI" | cut -c1-16)
|
||||
current_fp="${image_ver}:${config_hash}"
|
||||
|
||||
# CMDEPLOY_STAGES non-empty in env = operator override -> always run.
|
||||
# Otherwise, if fingerprint matches the last successful deploy, skip.
|
||||
if [ -z "${CMDEPLOY_STAGES:-}" ] \
|
||||
&& [ -f "$FINGERPRINT_FILE" ] \
|
||||
&& [ "$(cat "$FINGERPRINT_FILE")" = "$current_fp" ]; then
|
||||
echo "[INFO] No changes detected ($current_fp), skipping deploy."
|
||||
else
|
||||
export CMDEPLOY_STAGES="${CMDEPLOY_STAGES:-configure,activate}"
|
||||
|
||||
# Skip DNS check when MAIL_DOMAIN is a bare IP address
|
||||
SKIP_DNS=""
|
||||
if [[ "$MAIL_DOMAIN" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] || [[ "$MAIL_DOMAIN" =~ : ]]; then
|
||||
SKIP_DNS="--skip-dns-check"
|
||||
fi
|
||||
$CMDEPLOY run --config "$CHATMAIL_INI" --ssh-host @local $SKIP_DNS
|
||||
|
||||
# Restore the build-time hash
|
||||
cp /etc/chatmail-image-version /etc/chatmail-version
|
||||
echo "$current_fp" > "$FINGERPRINT_FILE"
|
||||
fi
|
||||
|
||||
# Signal success to Docker healthcheck
|
||||
touch /run/chatmail-init.done
|
||||
|
||||
# Forward journald to console so `docker compose logs` works
|
||||
grep -q '^ForwardToConsole=yes' /etc/systemd/journald.conf \
|
||||
|| echo "ForwardToConsole=yes" >> /etc/systemd/journald.conf
|
||||
systemctl restart systemd-journald
|
||||
110
docker/chatmail_relay.dockerfile
Normal file
110
docker/chatmail_relay.dockerfile
Normal file
@@ -0,0 +1,110 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
FROM jrei/systemd-debian:12 AS base
|
||||
|
||||
ENV LANG=en_US.UTF-8
|
||||
|
||||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
||||
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
|
||||
echo 'APT::Install-Recommends "0";' > /etc/apt/apt.conf.d/01norecommend && \
|
||||
echo 'APT::Install-Suggests "0";' >> /etc/apt/apt.conf.d/01norecommend && \
|
||||
apt-get update && \
|
||||
DEBIAN_FRONTEND=noninteractive TZ=UTC \
|
||||
apt-get install -y \
|
||||
ca-certificates \
|
||||
gcc \
|
||||
git \
|
||||
python3 \
|
||||
python3-dev \
|
||||
python3-venv \
|
||||
tzdata \
|
||||
locales && \
|
||||
sed -i -e "s/# $LANG.*/$LANG UTF-8/" /etc/locale.gen && \
|
||||
dpkg-reconfigure --frontend=noninteractive locales && \
|
||||
update-locale LANG=$LANG
|
||||
|
||||
# --- Build-time: install cmdeploy venv and run install stage ---
|
||||
# Editable install so importlib.resources reads directly from the source tree.
|
||||
# On container start only "configure,activate" stages run.
|
||||
|
||||
# Copy dependency metadata first so pip install layer is cached
|
||||
COPY cmdeploy/pyproject.toml /opt/chatmail/cmdeploy/pyproject.toml
|
||||
COPY chatmaild/pyproject.toml /opt/chatmail/chatmaild/pyproject.toml
|
||||
|
||||
# Dummy scaffolding so editable install can discover packages
|
||||
RUN mkdir -p /opt/chatmail/cmdeploy/src/cmdeploy \
|
||||
/opt/chatmail/chatmaild/src/chatmaild && \
|
||||
touch /opt/chatmail/cmdeploy/src/cmdeploy/__init__.py \
|
||||
/opt/chatmail/chatmaild/src/chatmaild/__init__.py
|
||||
|
||||
# Dummy git repo: .git/ is excluded from the build context (.dockerignore)
|
||||
# but setuptools calls `git ls-files` when building the sdist.
|
||||
WORKDIR /opt/chatmail
|
||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||
git init -q && \
|
||||
python3 -m venv /opt/cmdeploy && \
|
||||
/opt/cmdeploy/bin/pip install -e chatmaild/ -e cmdeploy/
|
||||
|
||||
# Full source copy (editable install's .egg-link still points here)
|
||||
COPY . /opt/chatmail/
|
||||
|
||||
# Minimal chatmail.ini
|
||||
RUN printf '[params]\nmail_domain = build.local\n' > /tmp/chatmail.ini
|
||||
|
||||
RUN CMDEPLOY_STAGES=install \
|
||||
CHATMAIL_INI=/tmp/chatmail.ini \
|
||||
CHATMAIL_NOSYSCTL=True \
|
||||
CHATMAIL_NOPORTCHECK=True \
|
||||
/opt/cmdeploy/bin/pyinfra @local \
|
||||
/opt/chatmail/cmdeploy/src/cmdeploy/run.py -y
|
||||
|
||||
RUN cp -a www/ /opt/chatmail-www/
|
||||
|
||||
# Remove build-only packages — not needed at runtime.
|
||||
# Keep git: test_deployed_state needs `git rev-parse HEAD` to verify the
|
||||
# deployed version hash matches /etc/chatmail-version.
|
||||
RUN apt-get purge -y gcc python3-dev && \
|
||||
apt-get autoremove -y && \
|
||||
rm -f /tmp/chatmail.ini
|
||||
|
||||
# Record image version (used in deploy fingerprint at runtime).
|
||||
# GIT_HASH is passed as a build arg (from docker-compose or CI) so that
|
||||
# .git/ can be excluded from the build context via .dockerignore.
|
||||
# Two files: chatmail-image-version is the immutable build hash (survives
|
||||
# deploys); chatmail-version is overwritten by cmdeploy run and restored
|
||||
# from the image version after each deploy in chatmail-init.sh.
|
||||
ARG GIT_HASH=unknown
|
||||
RUN echo "$GIT_HASH" > /etc/chatmail-image-version && \
|
||||
echo "$GIT_HASH" > /etc/chatmail-version
|
||||
|
||||
# Mock git HEAD so `git rev-parse HEAD` returns the source repo's commit hash.
|
||||
# The .git/ dir was created by `git init` earlier (for setuptools); we just
|
||||
# write the build hash into whatever branch HEAD points to.
|
||||
RUN head_ref=$(sed 's/^ref: //' /opt/chatmail/.git/HEAD) && \
|
||||
mkdir -p "/opt/chatmail/.git/$(dirname "$head_ref")" && \
|
||||
echo "$GIT_HASH" > "/opt/chatmail/.git/$head_ref"
|
||||
# --- End build-time install ---
|
||||
|
||||
ENV TZ=:/etc/localtime
|
||||
ENV PATH="/opt/cmdeploy/bin:${PATH}"
|
||||
RUN ln -s /etc/chatmail/chatmail.ini /opt/chatmail/chatmail.ini
|
||||
|
||||
ARG CHATMAIL_INIT_SERVICE_PATH=/lib/systemd/system/chatmail-init.service
|
||||
COPY ./docker/chatmail-init.service "$CHATMAIL_INIT_SERVICE_PATH"
|
||||
RUN ln -sf "$CHATMAIL_INIT_SERVICE_PATH" "/etc/systemd/system/multi-user.target.wants/chatmail-init.service"
|
||||
|
||||
# Remove default nginx site config at build time (not in entrypoint)
|
||||
RUN rm -f /etc/nginx/sites-enabled/default
|
||||
|
||||
COPY --chmod=555 ./docker/chatmail-init.sh /chatmail-init.sh
|
||||
COPY --chmod=555 ./docker/entrypoint.sh /entrypoint.sh
|
||||
COPY --chmod=555 ./docker/healthcheck.sh /healthcheck.sh
|
||||
|
||||
HEALTHCHECK --interval=10s --start-period=180s --timeout=10s --retries=3 \
|
||||
CMD /healthcheck.sh
|
||||
|
||||
STOPSIGNAL SIGRTMIN+3
|
||||
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
|
||||
CMD [ "--default-standard-output=journal+console", \
|
||||
"--default-standard-error=journal+console" ]
|
||||
11
docker/docker-compose.ci.yaml
Normal file
11
docker/docker-compose.ci.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
# Used by .github/workflows/docker-ci.yaml
|
||||
# The GHCR image is set via CHATMAIL_IMAGE env var at deploy time.
|
||||
services:
|
||||
chatmail:
|
||||
image: ${CHATMAIL_IMAGE:-chatmail-relay:latest}
|
||||
volumes:
|
||||
- /srv/chatmail/chatmail.ini:/etc/chatmail/chatmail.ini
|
||||
- /srv/chatmail/dkim:/etc/dkimkeys
|
||||
- /srv/chatmail/certs:/var/lib/acme
|
||||
environment:
|
||||
TLS_EXTERNAL_CERT_AND_KEY: /var/lib/acme/live/${MAIL_DOMAIN}/fullchain /var/lib/acme/live/${MAIL_DOMAIN}/privkey
|
||||
44
docker/docker-compose.override.yaml.example
Normal file
44
docker/docker-compose.override.yaml.example
Normal file
@@ -0,0 +1,44 @@
|
||||
# Local overrides: copy to docker-compose.override.yaml in this directory.
|
||||
# Compose automatically merges this with docker-compose.yaml.
|
||||
#
|
||||
# cp docker-compose.override.yaml.example docker-compose.override.yaml
|
||||
#
|
||||
# Volumes are APPENDED to the base file's volumes list, environment and other scalar keys are MERGED by key.
|
||||
services:
|
||||
chatmail:
|
||||
volumes:
|
||||
## Data paths — bind-mount to host directories for easy access/backup.
|
||||
|
||||
# - ./data/dkim:/etc/dkimkeys
|
||||
# - ./data/certs:/var/lib/acme
|
||||
|
||||
# - ./data/mail:/home/vmail
|
||||
## Or mount from an existing bare-metal install.
|
||||
# - /home/vmail:/home/vmail
|
||||
|
||||
## Mount your own chatmail.ini (skips auto-generation):
|
||||
# - ./chatmail.ini:/etc/chatmail/chatmail.ini
|
||||
|
||||
## Custom website:
|
||||
# - ./custom/www:/opt/chatmail-www
|
||||
|
||||
## Debug — mount scripts for live editing:
|
||||
# - ./chatmail-init.sh:/chatmail-init.sh
|
||||
# - ./entrypoint.sh:/entrypoint.sh
|
||||
|
||||
# environment:
|
||||
## Mount certs (above) and set TLS_EXTERNAL_CERT_AND_KEY to in-container paths.
|
||||
## A tls-cert-reload.path watcher inside the container reloads services
|
||||
## when the cert file changes. However, inotify does not cross bind-mount
|
||||
## boundaries, so host-side renewals (certbot, acmetool, etc.) must
|
||||
## notify the container explicitly. Add this to your renewal hook:
|
||||
##
|
||||
## docker exec chatmail systemctl start tls-cert-reload.service
|
||||
##
|
||||
## Host acmetool (bare-metal migration): create mount above, and
|
||||
## rsync -a /var/lib/acme/live data/certs
|
||||
# TLS_EXTERNAL_CERT_AND_KEY: "/var/lib/acme/live/${MAIL_DOMAIN}/fullchain /var/lib/acme/live/${MAIL_DOMAIN}/privkey"
|
||||
##
|
||||
## (Untested) Traefik certs-dumper (see docker/docker-compose-traefik.yaml) - also add volume:
|
||||
## - traefik-certs:/certs:ro
|
||||
# TLS_EXTERNAL_CERT_AND_KEY: "/certs/${MAIL_DOMAIN}/certificate.crt /certs/${MAIL_DOMAIN}/privatekey.key"
|
||||
48
docker/docker-compose.yaml
Normal file
48
docker/docker-compose.yaml
Normal file
@@ -0,0 +1,48 @@
|
||||
# Base compose file — do not edit. Put customizations (data paths, extra
|
||||
# volumes, env overrides) in docker-compose.override.yaml instead.
|
||||
# See docker-compose.override.yaml.example in this directory for a starting point.
|
||||
#
|
||||
# Security notes: this container uses
|
||||
# - network_mode:host chatmail needs many ports (25, 53, 80, 143, 443, 465,
|
||||
# 587, 993, 3340, 8443) and needs to operate from the real IP, which bridging
|
||||
# would make tricky
|
||||
# - cgroup:host (required for systemd).
|
||||
# Together these give the container near-host-level access. This is acceptable
|
||||
# for a dedicated mail server, but be aware that the container can bind any
|
||||
# port and see all host network traffic.
|
||||
|
||||
services:
|
||||
chatmail:
|
||||
build:
|
||||
context: ../
|
||||
dockerfile: docker/chatmail_relay.dockerfile
|
||||
args:
|
||||
GIT_HASH: ${GIT_HASH:-unknown}
|
||||
image: chatmail-relay:latest
|
||||
restart: unless-stopped
|
||||
container_name: chatmail
|
||||
# Required for systemd — use only one of the following:
|
||||
cgroup: host # compose v2
|
||||
# privileged: true # compose v1 (less restricted)
|
||||
tty: true # required for logs
|
||||
tmpfs: # required for systemd
|
||||
- /tmp
|
||||
- /run
|
||||
- /run/lock
|
||||
logging:
|
||||
driver: none
|
||||
environment:
|
||||
MAIL_DOMAIN: $MAIL_DOMAIN
|
||||
network_mode: "host"
|
||||
volumes:
|
||||
## system (required)
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
## data (defaults — override in docker-compose.override.yaml)
|
||||
- mail:/home/vmail
|
||||
- dkim:/etc/dkimkeys
|
||||
- certs:/var/lib/acme
|
||||
|
||||
volumes:
|
||||
mail:
|
||||
dkim:
|
||||
certs:
|
||||
9
docker/entrypoint.sh
Executable file
9
docker/entrypoint.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
set -eo pipefail
|
||||
|
||||
CHATMAIL_INIT_SERVICE_PATH="${CHATMAIL_INIT_SERVICE_PATH:-/lib/systemd/system/chatmail-init.service}"
|
||||
|
||||
env_vars="MAIL_DOMAIN CMDEPLOY_STAGES CHATMAIL_INI TLS_EXTERNAL_CERT_AND_KEY PATH"
|
||||
sed -i "s|<envs_list>|$env_vars|g" "$CHATMAIL_INIT_SERVICE_PATH"
|
||||
|
||||
exec /lib/systemd/systemd "$@"
|
||||
1
docker/env.example
Normal file
1
docker/env.example
Normal file
@@ -0,0 +1 @@
|
||||
MAIL_DOMAIN=chat.example.com
|
||||
16
docker/healthcheck.sh
Normal file
16
docker/healthcheck.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
# returns 0 when chatmail-init succeeded and all expected services are running.
|
||||
|
||||
set -e
|
||||
|
||||
test -f /run/chatmail-init.done
|
||||
|
||||
# Core services
|
||||
services="chatmail-metadata doveauth dovecot filtermail filtermail-incoming nginx postfix unbound"
|
||||
|
||||
# Optional services
|
||||
for svc in iroh-relay turnserver; do
|
||||
systemctl is-enabled "$svc" 2>/dev/null && services="$services $svc"
|
||||
done
|
||||
|
||||
exec systemctl is-active $services
|
||||
Reference in New Issue
Block a user