In the previous lab, “Virtual Sensors for Data Processing”, you developed Python scripts to simulate virtual sensors for the Smart Trash Can project. This virtual sensor processed data from a PIR motion sensor and a synthetic volume sensor to determine the fill percentage of a trash can. The next step involves deploying these virtual sensors using Docker containers. This will help you gain insight into modern deployment practices commonly used in the IoT industry, ensuring that your code is portable, scalable, and easy to manage.
What is Docker?
Docker is a platform for developing, shipping, and running applications inside lightweight, portable containers. A container includes your application code, dependencies, and runtime environment. By using containers, you can ensure that your application runs consistently across different machines and operating systems.
Why Use Docker for Virtual Sensors?
- Isolation: Each virtual sensor runs in its own environment without interfering with others.
- Reproducibility: Docker ensures your application behaves identically in different environments.
- Scalability: Need more sensors? Spin up multiple container instances with ease.
Step-by-Step Guide
Prerequisites
-
Docker Installation:
Ensure Docker is installed on your system. If you followed previous labs, you likely already have Docker set up. -
MQTT & Home Assistant Setup:
Make sure your Home Assistant and MQTT broker (e.g., Mosquitto) are running as discussed in previous labs.
Create a Dockerfile
A Dockerfile is a simple text file that contains the commands needed to build a Docker image. We’ll create one for the virtual sensor script that calculates the fill percentage of the trash can.
Example Dockerfile
for trash_can_virtual_sensor.py
:
# Start with a lightweight Python image
FROM python:3.8-slim
# Set a working directory in the container
WORKDIR /usr/src/app
# Copy the virtual sensor script into the container
COPY trash_can_virtual_sensor.py ./
# Install wget and any other dependencies if needed
RUN apt-get update && apt-get install -y wget
# Download and install paho-mqtt (or install via pip if you prefer)
RUN wget https://files.pythonhosted.org/packages/source/p/paho-mqtt/paho-mqtt-1.5.0.tar.gz \
&& tar -xzvf paho-mqtt-1.5.0.tar.gz \
&& cd paho-mqtt-1.5.0 \
&& python setup.py install
# Set the command to run the virtual sensor script when the container starts
CMD ["python", "./trash_can_virtual_sensor.py"]
Explanation
- Base Image: We use a lightweight Python image.
- Copy Script: The virtual sensor script (
trash_can_virtual_sensor.py
) is copied into the container. - Dependencies: Install
paho-mqtt
for MQTT communication. - Start Command: The container runs the Python script when started.
Build the Docker Image
Use the docker build
command to create an image from the Dockerfile:
docker build -t trash_can_virtual_sensor .
Run the Container
Start a container from your newly built image:
docker run -d --name trash_can_vs_container trash_can_virtual_sensor
Explanation
-d
: Runs the container in detached mode (background).--name
: Names the containertrash_can_vs_container
.
Your virtual sensor is now running inside a container, processing volume and motion data, and publishing fill percentage results to MQTT for Home Assistant to display.
Monitoring and Logs
To view logs from the container:
docker logs trash_can_vs_container
Home Assistant Backup / Migration with Docker Volumes
Now that you have your virtual sensors containerized, consider how to preserve and migrate your Home Assistant setup. For Home Assistant, MQTT configurations, and other persistent data, Docker volumes are crucial.
Why Use Docker Volumes?
- Persistence: Volumes store persistent data (e.g., Home Assistant configuration, database files, etc.). Without volumes, data would be lost when containers are removed.
- Easy Backup and Migration: Volumes can be backed up and restored, simplifying the process of moving Home Assistant to a new host or restoring after a disaster.
- Data Sharing: Volumes allow multiple containers to share the same data consistently.
Example: Using Volumes with Home Assistant
In docker-compose.yml
, Home Assistant might look like this:
services:
homeassistant:
image: ghcr.io/home-assistant/home-assistant:stable
volumes:
- ha-config:/config
restart: unless-stopped
volumes:
ha-config:
Explanation
ha-config
is a Docker volume storing Home Assistant’s configuration and state.
Backing Up Volumes
To back up data from a named volume, use a temporary container:
docker run --rm -v ha-config:/data -v $(pwd):/backup ubuntu tar -czvf /backup/ha-config-backup.tar.gz -C /data ./
Restoring Volumes
To restore from a backup:
docker run --rm -v ha-config:/data -v $(pwd):/backup ubuntu tar -xzvf /backup/ha-config-backup.tar.gz -C /data
Using Backups for Migration
- Copy the Backup: Copy the
tar.gz
file to the new machine. - Create a Volume: Create a volume
ha-config
on the new machine. - Restore Backup: Restore the backup into the volume.
- Start Home Assistant: Use
docker-compose
to start Home Assistant with the restored volume.