4.5 KiB
Watchtower
Monitor, automate and update Docker container images.
Its different ways to configure watchtower and mine are set up to monitor ALL containers and only exclude the ones I choose.
The template file is used to format the status email once the check has concluded.
Prerequisites
The following modules are required for the script to function. They will need to be installed first. Python ships with some core modules but others will need to be installed separately.
Standard Python Library Modules:
This script uses Python and multiple add on modules.
-
os: Provides a way to use operating system dependent functionality. Part of the standard library.
-
time: Used for time-related tasks. Included in the standard library.
-
json: Used for JSON serialization and deserialization. Part of the standard library.
-
logging: Used for logging events and messages. Standard library module.
-
datetime: Provides classes for manipulating dates and times. Standard library module.
-
subprocess: Used for running new applications or programs through Python. Part of the standard library.
-
logging.handlers: A component of the logging module, offers various handlers for logging. Standard library.
-
configparser: Renamed to ConfigParser in Python 3, this module is used for working with configuration files. Part of the standard library.
-
email.mime.text: Used for creating MIME objects of major type text. Part of the standard Python library under the email package.
Modules that Need to be Installed:
-
colorlog: This is an external library for colored logging output. You need to install it separately using pip.
-
requests: A popular HTTP library for Python, used for making HTTP requests. This is not included in the standard library and must be installed separately.
To install the external modules you will first need PIP
installed on your server. PIP is the package manager for Python.
-
See if pip is already installed. Open a terminal and type:
pip -V
-
If not, install it.
python get-pip.py
-
Once pip is installed you can install the external modules.
pip install colorlog requests
Folder Structure
If you have not done so already, create a new folder called scripts
in your ~home folder. This is where all your scripts will go.
Inside your scripts
folder create a new folder called ip_checker
When done your folders structure should look like this: /home/[username]/scripts/ip_checker
Script
Get the source code from this repo and save it as ip_checker.py
in the ip_checker folder created in the previous step.
You will also need to get the config file called ip_checker_config.ini
. The .ini file will hold the credentials for pushover and ntfy. You can also set your notification preference for the script.
Next we need to change the permissions of these files so that the server can read them.
-
For both
ip_checker.py
andip_checker_config.ini
right click then Properties. -
Click on
Permissions
tab at top. -
Make sure Owner & Group reflect your
username
withRead and Write
access to all. -
Check the
Execute
box to allow executing the file as a program. -
Close out the window.
Run
Now we need to test and run the script.
Make sure your terminal is open to the location of the ip_checker.py
file. Should be ~/scripts/ip_checker
Normally to run a Python script will would use python3 ip_checker.py
This will still work but because we have shebang set in the script we can just use
./ip_checker.py
You should now see [INFO]
log with current VPN IP of the qbittorrent-openvpn container. In the script we set the run time with interval_seconds = 60
. This setting tells the script to check the VPN IP address of the container every minute.
Automation
Now the script will need to be automated so that it starts the check continuously in the background. See system services to do so.