monit-docker

MONIT-DOCKER / v0.0.64

Configuration and rules

Name Docker connections, select containers and reuse readable rules.

1. Create a YAML file

Save this as monit-docker.yml. Replace web* with your container pattern.

clients:
  local:
    config:
      base_url: unix:///var/run/docker.sock
ctn-groups:
  web:
    match:
      - 'name:web*'
conditions:
  memory_high:
    expr:
      - mem_percent > 90
commands:
  graceful_restart:
    exec:
      - restart:
          kwargs:
            timeout: 10

2. Validate without Docker

monit-docker -c ./monit-docker.yml --client local --ctn-group web \
  check-config --cmd-if '@memory_high ? @graceful_restart' --output json

check-config checks structure and references without connecting to Docker or performing actions. It requires a file or nonempty inline configuration; a file containing {} is a valid minimal configuration. A completely empty YAML file is not.

3. Use aliases

monit-docker -c ./monit-docker.yml --client local --ctn-group web cron \
  --state-file "$HOME/.local/state/monit-docker/web.json" \
  --cooldown 300 --dry-run \
  --cmd-if '@memory_high ? @graceful_restart'

Understand a condition

mem_percent > 90 ? restart connects a condition to an action. Expressions within an alias are ANDed. Multiple --cmd-if options may each trigger their own sequence; avoid conflicting rules.

mem_usage is measured in bytes and accepts thresholds such as 100MiB. cpu_percent may exceed 100 across multiple cores. monit performs a simple check without persistent cooldown; use cron or serve for persistent state.

Paths and variables

The default path is /etc/monit-docker/monit-docker.yml. Set it with -c or MONIT_DOCKER_CONFFILE. MONIT_DOCKER_CONFIG supplies inline YAML when the file does not exist; an existing file takes precedence.

MONIT_DOCKER_LOGFILE and MONIT_DOCKER_RUNTIMEDIR control log and runtime paths. The log parent directory must exist. Select persistent state separately with --state-file.

Mako configuration templates are trusted code: do not load configuration from unknown sources.