Ultimate Network Monitoring Solution with a Raspberry Pi using Docker
Are you having a Raspberry Pi lying around in your home or you are still one of those like me who took a month to decide what to do with a Pi other than NAS or a PiHole? Then this blog will find you a right plan to not just monitor your network, block advertisements and many more. Expand as you plan with Docker Engine configured right on to your raspberry pi.
In this project, the main aim will be to make a standalone docker environment over a raspberry pi (2GB or above) and get started with network monitoring, advertisement blocking for your entire home network, Portainer and more. Before we begin, make sure you have setup at least a 32GB SD Card with Raspbian OS.
Minimum Requirements
- Raspberry Pi 3/4 — At least 2GB
- Micro SD Card — 32GB or more recommended
- Wired network connection to your raspberry pi, hence LAN cable
Here’s a quick video that can get you started with your Raspberry Pi Setup.
Once you are done with the setup, make sure you enable SSH access on the raspberry pi. This will allow us to heedlessly run the system and access it remotely over the network.
- Launch
Raspberry Pi Configuration
from thePreferences
menu - Navigate to the
Interfaces
tab - Select
Enabled
next toSSH
- Click
OK
Now you are ready to start off with the configuration. Please collect the IP address of the raspberry pi so that we can login to it. It would be good if you can fix the IP of the raspberry pi to be constant either via a DHCP static mapping on your router or configuring the raspbian’s network to be Static IP using your current network properties.
Initial Configuration
Assuming you have completed the aforementioned steps, the following methods can be applicable to a raspberry pi and also any virtual machine or physical machine running ubuntu-server or Debian operating system on it. Staying on the latest LTS release would be a safe option.
Firstly, SSH into your raspberry pi from any system on your network using ssh pi@your-raspberrypi-ip-address
. The default password of a raspberry pi running raspbian os is “raspberrypi”, unless you have changed it.
Once you are logged it, let's stay on the latest updates by running the following commands -
sudo apt-get update && sudo apt-get upgrade
The above will grant you super user access and get you the latest updates for your raspberry. All the commands here apply for any Ubuntu/Debian virtual machine too.
Installing Docker
Docker is a tool for creating, deploying, and running applications in containers. The software is popular among developers as it speeds up the development process and does not use a lot of resources.
curl -fsSL https://get.docker.com -o get-docker.shsudo sh get-docker.shsudo usermod -aG docker Pi
The above three commands will install the latest version of docker and provision the user “pi” to have access over the docker engine.
To check if docker is installed properly, run —
docker version
The output should display the Docker version along with some additional information. If that is so, then you have successfully installed docker.
Installing Docker Compose
Run the following commands one-by-one to get docker-compose installed. Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-compose
After the same is done, run the following command to generate a similar output.
$ docker-compose --version
docker-compose version 1.29.2, build 1110ad01
If docker-compose fails, you need to create a symbolic link by doing the following -
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Voila, your docker setup is ready. In the next steps, we will install PiHole and a Custom Docker Stack that will help you monitor your network stats. Get granular ideas about your network regarding the speed, latency, jitter and many more with this.
In this way you will know if your ISP gives you promised speeds all the time.
Installing Internet-Monitoring Stack
This is a custom docker app running Prometheus and Grafana that will give you real-time visualizations of your network status.
Head on to the following repository to get started.
If this helped you, a star⭐ to the repo will be encouraging.
The readme briefs about all the configuration possibilities, however, to keep this article short, we will go through the nominal steps to get you started.
Login to your raspberry-pi and enter the following -
git clone https://github.com/dipan29/internet-monitoring
cd internet-monitoring/prometheus
sudo docker-compose up -d
Go to the IP address of your raspberry-pi http://192.168.x.xxx:3030/d/o9mIe_Aik/internet-connection and enter the following credentials to view the dashboard.
Username — admin
Password — monitoring
The password can be later changed through Grafana dashboard; however, the password is stored in grafana/config.monitoring file.
The system will do a speedtest every 15 minutes, configurable in prometheus/prometheus.yml file and check ping to hosts or websites (HTTP 200 OK response time) to sites mentioned in prometheus/pinghosts.yml file.
Configure the parameters as deemed necessary, and re-instate the containers with the following -
cd internet-monitoring/prometheus
sudo docker-compose up -d
You are ready to rock now, The DataSource and Dashboard for Grafana are automatically provisioned. The Grafana Dashboard can be accessed directly from http://192.168.x.xxx:3030/ where 192.168.x.xxx is the IP address of your raspberry pi or your virtual machine.
Installing Pi-hole — Network wide Ad-Blocker
Pi-Hole is a popular network wide ad-blocker that is basically a proxy to your current DNS servers which blocks the IPs/hosts that provides advertisements and also the hosts that tracks you. In case they are found on a website, as they are blocked, they won’t be rendered on the page. The blacklists are updated regularly OTA.
Login to your raspberry pi and run the following commands -
git clone https://github.com/pi-hole/docker-pi-hole.git
cd docker-pi-hole
cp docker-compose.yml.example docker-compose.yml
nano docker-compose.yml
A text-editor will open showing all the entries of docker-compose.yml file. Edit your timezone (lookup google for your timezone) to your current “Continent/City” format. Save and exit the text editor by using Ctrl+W and Ctrl+X.
Run the following to start your pi hole container.
docker-compose up --detach
Run docker logs pihole | grep random
to find your random administration password. Or if you want to set your password before starting your container, uncomment the # WEBPASSWORD: line and set a secure password in ‘single-quotes’ before running docker-compose up --detach
after saving the file.
Head on to http://192.168.x.xxx/ to get started with the PiHole dashboard, where 192.168.x.xxx is the IP address of your pi.
To log on to admin panel, enter the provided password or the random generated password at http://192.168.x.xxx/admin/ to view the requests, manage the service, view blacklists and more.
Once you are done, go to your router and set the Preferred DNS server IP to be 192.168.x.xxx (the ip address of the pi), while keeping the Secondary DNS intact.
Pro Tip — Never try to expose your DNS servers on the internet, especially port 53. You may expose your http:80 port, but if you expose dns:53, your PiHole will have massive DNS attacks and fail due to overload.
Congratulations, your pi-hole DNS service is now usable at your home network. Enjoy add-free experience on your local network, your mileage may vary ;-).
Bonus: Installing Portainer
Portainer is an application that provides a GUI to manage your docker environment over the web, making your life easier.
You can control your containers, manage environment variables, install docker images and more right from the dashbord without needing to look back into the ssh console unless something fails.
Run the following commands to quickly get started with portainer.
docker volume create portainer_datadocker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
The above will set-up your portainer container that can be accessed from http://192.168.x.xxx:9000/ where 192.168.x.xxx is the IP of your virtual machine or raspberry pi.
During the first login, it will ask you to create your admin credentials, make sure you keep a strong and memorable password.
Cheers, that comes to an end for this article, but stay tuned for more articles on docker. Keep looking for docker images and keep trying them out as your 4GB RPI can handle quite more than what you would expect from your site.
If you have any queries, or wish to connect with me, head on to -
Socials — LinkedIn YouTube Twitter
See you, next time!
Additional Resources/References -
- Docker Documentation Install instructions for Raspbian
- Repository Fork of Maxandersen
- Pi-hole
- Portainer Docs