This Docker container provides a Telegram integration to notify you about Docker events. It can notify you when a container starts, stops (including details about exit codes), restarts, and when the healthcheck status of a Docker container changes. You have the flexibility to customize these notifications by modifying the templates.js
file.
This fork was created to address security vulnerabilities and add support for
linux/arm64
andlinux/arm/v7
in addition tolinux/amd64
.
If you encounter any issues, please feel free to contribute by fixing them and opening a pull request or reporting a new issue.
-
Set up a Telegram bot
- create a Telegram bot and obtain the Bot Token
- optionally add the bot to a group and allow it to post messages
- extract the Chat ID
-
Run the container
using
docker-compose.yaml
services: telegram-notifier: image: lorcas/docker-telegram-notifier:latest volumes: - /var/run/docker.sock:/var/run/docker.sock:ro # for local instance environment: TELEGRAM_NOTIFIER_BOT_TOKEN: <bot_token> TELEGRAM_NOTIFIER_CHAT_ID: <chat_id>
using
docker run
docker run -d \ --env TELEGRAM_NOTIFIER_BOT_TOKEN=<bot_token> \ --env TELEGRAM_NOTIFIER_CHAT_ID=<chat_id> \ --volume /var/run/docker.sock:/var/run/docker.sock:ro \ --hostname my_host \ lorcas/docker-telegram-notifier
-
Add a healthcheck to your container (optional)
example: image: hello-world healthcheck: test: ["CMD", "curl", "-sS", "http://127.0.0.1:8545", "||", "exit", "1"] interval: 30s timeout: 10s retries: 3
This setup will start the container and notify you about Docker events. For more advanced configuration, see the Advanced setup section.
The following options are available to customize the behavior of the notifier. Examples are provided for docker-compose.yaml
but are also applicable to docker run
. Only the changes are shown, make sure to include the rest from the Basic setup section.
Use TELEGRAM_NOTIFIER_TOPIC_ID
or TELEGRAM_NOTIFIER_THREAD_ID
for specific topics/threads:
services:
telegram-notifier:
environment:
TELEGRAM_NOTIFIER_TOPIC_ID: <topic_id> # optional use only one
TELEGRAM_NOTIFIER_THREAD_ID: <thread_id> # optional use only one
Disable notifications for specific containers:
services:
example:
image: hello-world
labels:
telegram-notifier.monitor: false
docker run
docker run -d --label telegram-notifier.monitor=false hello-world
Receive notifications only from whitelisted containers by setting ONLY_WHITELIST=true
and labeling desired containers:
services:
telegram-notifier:
environment:
ONLY_WHITELIST: true
example:
image: hello-world
labels:
telegram-notifier.monitor: true
docker run
docker run -d --label telegram-notifier.monitor=true hello-world
Configure different channels/threads per container:
services:
example:
image: hello-world
labels:
# Channel override (optional)
telegram-notifier.chat-id: "-100123456789"
# Thread/Topic override (optional - use only one)
telegram-notifier.topic-id: "12345"
telegram-notifier.thread-id: "12345"
docker run
docker run -d --label telegram-notifier.chat-id=-100123456789 --label telegram-notifier.topic-id=12345 hello-world
By default notifier connects to a local docker instance (don't forget to specify --volume /var/run/docker.sock:/var/run/docker.sock:ro
for this case). But if you have monitoring and the service on the same host, you will not receive notifications if the host goes down. So I recommend to have monitoring separately.
Notifier accepts usual DOCKER_HOST
and DOCKER_CERT_PATH
environment variables to specify remote instance. For http endpoint you need to specify only --env DOCKER_HOST=tcp://example.com:2375
(make sure to keep such instances behind the firewall). For https, you'll also need to mount a volume with https certificates that contains ca.pem
, cert.pem
, and key.pem
: --env DOCKER_HOST=tcp://example.com:2376 --env DOCKER_CERT_PATH=/certs --volume $(pwd):/certs
.
A tutorial on how to generate docker certs can be found here.
services:
telegram-notifier:
volumes:
# disable for remote ONLY monitoring
# - /var/run/docker.sock:/var/run/docker.sock:ro
- ./certs:/certs # for remote instance
environment:
DOCKER_HOST: tcp://example.com:2376 # http/https is detected by port number
DOCKER_CERT_PATH: /certs # should contain ca.pem, cert.pem, key.pem
-
Adapt the template: download and modify the message strings from
templates.js
according to your needs. -
Bind your customized file to the container:
using
docker-compose.yaml
services: notifier: volumes: # Bind customized file to templates.js in the container: - ./my-template.js:/usr/src/app/templates.js:ro environment: # ...
docker run
docker run -d \ --env TELEGRAM_NOTIFIER_BOT_TOKEN=token \ --env TELEGRAM_NOTIFIER_CHAT_ID=chat_id \ --volume /var/run/docker.sock:/var/run/docker.sock:ro \ --volume ./my-template.js:/usr/src/app/templates.js:ro \ --hostname my_host \ lorcas/docker-telegram-notifier
Here are some variables available to customize the notification messages.
Variable | Description |
---|---|
${e.Actor.Attributes.name} |
Container name |
${e.Actor.Attributes.container} |
Container ID |
${e.Actor.Attributes.image} |
Container image used |
${e.Actor.Attirbutes.exitCode} |
Container exit code |
Example:
container_start: e =>
`✅ Container Started\n` +
`Name: <b>${e.Actor.Attributes.name}</b>\n` +
`Image: <code>${e.Actor.Attributes.image}</code>\n` +
`ID: <code>${e.Actor.Attributes.container}</code>`
π’ Container Started
Name: my-container
Image: nginx:latest
ID: abc123def456
The following variables are only available if the container was started using docker compose
Variable | Description |
---|---|
${e.Actor.Attributes['com.docker.compose.container-number']} |
Compose container Number |
${e.Actor.Attributes['com.docker.compose.project']} |
Compose Project Name |
${e.Actor.Attributes['com.docker.compose.service']} |
Compose Service Name |
${e.Actor.Attributes['com.docker.compose.version']} |
Compose Version |
Example:
container_start: e =>
`✅ Container Started\n` +
`Project: <b>${e.Actor.Attributes['com.docker.compose.project']}</b>\n` +
`Service: <b>${e.Actor.Attributes['com.docker.compose.service']}</b> (#${e.Actor.Attributes['com.docker.compose.container-number']})\n` +
`Image: <code>${e.Actor.Attributes.image}</code>\n` +
`Compose Version: <code>${e.Actor.Attributes['com.docker.compose.version']}<code>`
π’ Container Started
Project: myproject
Service: webserver (#1)
Image: nginx:latest
Compose Version: 2.17.2
Leverage the labels:
defintion on docker services to make custom information available to notification messages:
-
Add custom labels to a container:
using
docker-compose.yaml
services: example: image: hello-world labels: # Monitor control telegram-notifier.monitor: true # Custom defined labels and information mycustom.telegram.container-info: "Access via http://myhost.com/"
using
docker run
:docker run -d \ --label "telegram-notifier.monitor=true" \ --label "mycustom.telegram.container-info=Access via http://myhost.com/" \ hello-world
-
Adapt your customized messages template:
container_start: e => `▶️ <b>${e.Actor.Attributes.name}</b> started\n` + `Image: <code>${e.Actor.Attributes.image}</code>\n` + ( ${e.Actor.Attributes['mycustom.telegram.container-info']} ? `NOTE: ${e.Actor.Attributes['mycustom.telegram.container-info']}` : '' )
This container is based on the container by poma, originally an idea of arefaslani.