Skip to content

Commit 33a2983

Browse files
committed
Improve Docker installation instructions and usage
While being at it, also adjust some other former path references, i.e. "completely get rid of `/opt`".
1 parent 861da73 commit 33a2983

File tree

7 files changed

+168
-173
lines changed

7 files changed

+168
-173
lines changed

DOCKER.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Running mqttwarn with Docker
2+
3+
If you would rather use `mqttwarn` without installing Python and the
4+
required libraries, you can run it as a [Docker container](https://www.docker.com/).
5+
You need to install only the Docker executable.
6+
7+
You can run the image as a service, i.e. in the background, or you can
8+
run it interactively, perhaps to help diagnose a problem.
9+
10+
## Interactively
11+
12+
In order to verify `mqttwarn` works appropriately with the current
13+
configuration, you might want to invoke it interactively.
14+
15+
The following commands assume you start from scratch and will guide you through
16+
the necessary steps needed to create a configuration and run it.
17+
18+
Within an empty folder, create a baseline `mqttwarn.ini` file:
19+
```shell
20+
docker run -it --rm --volume=$PWD:/etc/mqttwarn jpmens/mqttwarn mqttwarn make-config > mqttwarn.ini
21+
touch samplefuncs.py
22+
```
23+
24+
Then, assuming your MQTT broker runs on `localhost`, amend the `mqttwarn.ini` like:
25+
```ini
26+
hostname = 'host.docker.internal'
27+
```
28+
29+
Now, invoke `mqttwarn`:
30+
```shell
31+
docker run -it --rm --volume=$PWD:/etc/mqttwarn jpmens/mqttwarn
32+
```
33+
34+
`Ctrl-C` will stop it. You can start and stop it as often as you like, here,
35+
probably editing the `.ini` file as you go.
36+
37+
Note that you can run a local copy of the image, if you've built one (see below),
38+
by replacing `jpmens/mqttwarn` with `mqttwarn-local` in the following examples.
39+
40+
41+
## As a service
42+
43+
This is the typical way of running `mqttwarn`.
44+
45+
From the folder containing your `mqttwarn.ini` file, run `mqttwarn` in the
46+
background:
47+
```shell
48+
docker run --name=mqttwarn --detach --rm --volume=$PWD:/etc/mqttwarn jpmens/mqttwarn
49+
```
50+
51+
Follow the log:
52+
```shell
53+
docker logs mqttwarn --follow
54+
```
55+
56+
To stop the container:
57+
```shell
58+
docker stop mqttwarn
59+
```
60+
61+
62+
## Options
63+
64+
### Configuration Location
65+
66+
Run the Docker image from anywhere by specifying a full path to the
67+
configuration file:
68+
```shell
69+
--volume=/full/path/to/folder:/etc/mqttwarn
70+
```
71+
72+
### Log file
73+
74+
By default, the log output will be sent to STDERR.
75+
76+
If you would like to log to a file on the host instead, add this to your
77+
`mqttwarn.ini` file:
78+
```ini
79+
logfile = '/log/mqttwarn.log'
80+
```
81+
Add this argument to `docker run`:
82+
```shell
83+
--volume=$PWD/log:/log
84+
```
85+
86+
`mqttwarn.log` will be created in your current folder, and appended to each
87+
time the container is executed. You can delete the file between subsequent
88+
invocations.
89+
90+
91+
### If your MQTT Broker is also running in Docker on the same host
92+
93+
If you give the MQTT broker container a name, then you can refer to it by name rather than by
94+
IP address. For instance, if it's named `mosquitto`, and started like
95+
```shell
96+
docker run --name=mosquitto -it --rm eclipse-mosquitto:1.6
97+
docker run --name=mosquitto -it --rm eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf
98+
```
99+
put this in your `mqttwarn.ini` file:
100+
```ini
101+
hostname = 'mosquitto'
102+
```
103+
Then add this argument to `docker run`:
104+
```shell
105+
--link=mosquitto
106+
```
107+
108+
### Custom services
109+
You have the option to inject services to the official container.
110+
If you have a `myservice.py` file, you can add it to the services directory with a new volume mount:
111+
```
112+
-v $PWD/myservice.py:/usr/local/lib/python3.8/site-packages/mqttwarn/services/myservice.py
113+
```
114+
After doing this, you can use your service like:
115+
```ini
116+
[config:myservice]
117+
```
118+
For service development you probably also want to change the loglevel to `DEBUG`.
119+
120+
121+
### A full example
122+
123+
```shell
124+
docker run -it --rm --volume=$PWD:/etc/mqttwarn --link=mosquitto jpmens/mqttwarn
125+
```
126+
127+
128+
## Build the image
129+
130+
If you are making any changes to the `mqttwarn` application or to the
131+
`Dockerfile`, you can build a local image from the files on your drive (not
132+
from the files on Github).
133+
134+
Execute the following from the root of the project :
135+
```
136+
docker build -t mqttwarn-local .
137+
```
138+
139+
You can then edit any files and rebuild the image as many times as you need.
140+
You don't need to commit any changes.
141+
142+
The name `mqttwarn-local` is not meaningful, other than making it obvious when
143+
you run it that you are using your own personal image. You can use any name you
144+
like, but avoid `mqttwarn` otherwise it's easily confused with the official
145+
images.

HANDBOOK.md

Lines changed: 2 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,158 +3404,9 @@ instead of waiting for the interval to elapse, you might want to configure:
34043404
pinger = 10.5; now=true
34053405
```
34063406
3407+
## Docker
34073408
3408-
## Running with Docker
3409-
3410-
If you would rather use `mqttwarn` without installing Python
3411-
and the required libraries, you can run it as a [Docker container](https://www.docker.com/).
3412-
You need to install only the Docker executable.
3413-
3414-
### Run the Image
3415-
3416-
You can run the image as a service, i.e. in the background, or you can
3417-
run it interactively, perhaps to help diagnose a problem.
3418-
3419-
Note that you can run a local copy of the image, if you've built one (see below),
3420-
by replacing `jpmens/mqttwarn` with `mqttwarn-local` in the following examples.
3421-
3422-
#### As a Service
3423-
3424-
This is the typical way of running `mqttwarn`.
3425-
3426-
From the folder containing your `mqttwarn.ini` file:
3427-
3428-
```
3429-
$ docker run -d --rm --name mqttwarn \
3430-
-v $PWD:/opt/mqttwarn/conf \
3431-
jpmens/mqttwarn
3432-
```
3433-
3434-
To stop the container:
3435-
```
3436-
$ docker stop mqttwarn
3437-
```
3438-
3439-
#### Interactively
3440-
3441-
If you want to experiment with your configuration, or to diagnose
3442-
a problem, it can be simpler to restart the Python script,
3443-
rather than to restart the Docker container.
3444-
3445-
From the folder containing your `mqttwarn.ini` file:
3446-
3447-
```
3448-
$ docker run -it --rm \
3449-
-v $PWD:/opt/mqttwarn/conf \
3450-
--entrypoint bash \
3451-
jpmens/mqttwarn
3452-
```
3453-
3454-
To start the application from within the container, just invoke
3455-
```
3456-
mqttwarn
3457-
```
3458-
3459-
`Ctrl-C` will stop it. You can start and stop it as often as you like, here, probably editing the `.ini` file as you go.
3460-
3461-
`Ctrl-D` or `exit` will stop the container.
3462-
3463-
3464-
3465-
#### Options
3466-
3467-
##### Configuration Location
3468-
3469-
You can of course run the Docker image from anywhere if you
3470-
specify a full path to the configuration file:
3471-
```
3472-
-v /full/path/to/folder:/opt/mqttwarn/conf
3473-
```
3474-
3475-
##### Functions
3476-
If you have one or more files of Python functions in the same folder
3477-
as your `.ini` file, then prefix
3478-
the filenames in `.ini` file with a folder:
3479-
```
3480-
functions = 'functions/funcs.py'
3481-
```
3482-
Then add this argument to `docker run`:
3483-
```
3484-
-v $PWD:/opt/mqttwarn/functions
3485-
```
3486-
3487-
##### Log file
3488-
3489-
By default the log file will be created inside the container.
3490-
If you would like instead log to a file on the host, add this to your
3491-
`mqttwarn.ini` file:
3492-
```
3493-
logfile = 'log/mqttwarn.log'
3494-
```
3495-
Add this argument to `docker run`
3496-
```
3497-
-v $PWD:/opt/mqttwarn/log
3498-
```
3499-
`mqttwarn.log` will be created in your current folder,
3500-
and appended to
3501-
each time the container is executed. You can delete the file
3502-
between executions.
3503-
3504-
Another solution is to write logs directly to stdout inside the container. That way they are piped to the docker logs engine and your terminal directly, without producing any files.
3505-
```
3506-
logfile = '/dev/stdout'
3507-
```
3508-
3509-
3510-
##### If your MQTT Broker is Also Running in Docker on the Same Host
3511-
3512-
If you give the MQTT broker container a name, then you can
3513-
refer to it by name rather than by
3514-
IP address. For instance, if it's named `mosquitto`
3515-
put this in your `mqttwarn.ini` file:
3516-
```
3517-
hostname = 'mosquitto'
3518-
```
3519-
Then add this argument to `docker run`:
3520-
```
3521-
--link mosquitto
3522-
```
3523-
3524-
##### A full example
3525-
3526-
If you have your `.ini` and Python files in your current directory,
3527-
this will run `mqttwarn` and place the log file in the current directory:
3528-
```
3529-
$ docker run -d --rm --name mqttwarn \
3530-
-v $PWD:/opt/mqttwarn/conf \
3531-
-v $PWD:/opt/mqttwarn/functions \
3532-
-v $PWD:/opt/mqttwarn/log \
3533-
--link mosquitto \
3534-
jpmens/mqttwarn
3535-
```
3536-
3537-
3538-
3539-
### Build the image
3540-
3541-
If you are making any changes to the `mqttwarn` application or to
3542-
the `Dockerfile`, you can build a local image from the files on your drive
3543-
(not from the files on Github).
3544-
3545-
Execute the following from the root of the project :
3546-
3547-
```
3548-
docker build -t mqttwarn-local .
3549-
```
3550-
3551-
You can then edit any files and rebuild the image as many times as you need.
3552-
You don't need to commit any changes.
3553-
3554-
The name `mqttwarn-local` is not meaningful, other than
3555-
making it obvious when you run it that you are using your
3556-
own personal image. You can use any name you like, but avoid
3557-
`mqttwarn` otherwise it's easily confused with the official images.
3558-
3409+
In order to run `mqttwarn` on Docker, please follow up at [DOCKER.md](DOCKER.md).
35593410
35603411
35613412
## Examples ##

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
########
2424
mqttwarn
2525
########
26+
2627
To *warn*, *alert*, or *notify*.
2728

2829
.. image:: https://raw.githubusercontent.com/jpmens/mqttwarn/master/assets/google-definition.jpg
@@ -33,6 +34,7 @@ To *warn*, *alert*, or *notify*.
3334
*****
3435
About
3536
*****
37+
3638
mqttwarn - subscribe to MQTT topics and notify pluggable services.
3739

3840

@@ -71,19 +73,22 @@ MQTT topic ``home/monitoring/+`` as notification via *e-mail* and *Pushover*.
7173

7274

7375
.. _handbook: https://github.com/jpmens/mqttwarn/blob/master/HANDBOOK.md
76+
.. _Docker handbook: https://github.com/jpmens/mqttwarn/blob/master/DOCKER.md
7477
.. _handbook_services: https://github.com/jpmens/mqttwarn/blob/master/HANDBOOK.md#supported-notification-services
7578

7679

7780
*************
7881
Documentation
7982
*************
83+
8084
The handbook_ is the right place to read all about *mqttwarn*'s
8185
features and service plugins.
8286

8387

8488
************
8589
Installation
8690
************
91+
8792
Synopsis::
8893

8994
pip install --upgrade mqttwarn
@@ -97,10 +102,19 @@ You can also add support for multiple services, all at once::
97102
pip install --upgrade 'mqttwarn[apprise,asterisk,nsca,osxnotify,tootpaste,xmpp]'
98103

99104

105+
***************
106+
Container image
107+
***************
108+
109+
For running ``mqttwarn`` on a container infrastructure like Docker or
110+
Kubernetes, there are images on Docker Hub called ``jpmens/mqttwarn``.
111+
To read more about this topic, please follow up on the `Docker handbook`_.
112+
100113

101114
*************
102115
Configuration
103116
*************
117+
104118
First, create configuration and custom Python starter files
105119
``mqttwarn.ini`` and ``samplefuncs.py`` and edit them to your taste::
106120

etc/mqttwarn.openrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/sbin/openrc-run
22

3-
command="/usr/bin/python /opt/mqttwarn/mqttwarn.py"
3+
command="/usr/local/bin/mqttwarn"
44
command_args="${MQTTWARN_OPTIONS}"
55
command_background=yes
66
pidfile=/run/mqttwarn.pid
7-
directory=/opt/mqttwarn
87

98
name="mqttwarn"
109
description="mqttwarn pluggable mqtt notification service"

0 commit comments

Comments
 (0)