Note: For examples of customization, another branch is available, which:
- Restricts wildcard searches across users (in a naive fashion where the first level of a topic is the username, with protections provided by an ACL file)
- Encrypts retained messages using gocryptfs
- Auto-saves retained messages after every message
You can find it here: protected-no-wildcard branch or view the diff instead.
This setup demonstrates how to configure and deploy an MQTT broker using Eclipse Mosquitto within a Docker container and securely expose it to the internet via a Cloudflare Tunnel. This setup is particularly useful for IoT applications, allowing secure and scalable message brokering over the MQTT protocol.
- https://en.wikipedia.org/wiki/MQTT
- https://mosquitto.org/
- https://www.cloudflare.com/
- https://www.cloudflare.com/products/tunnel/
The following steps will guide you through the process of setting up an MQTT broker, configuring a Cloudflare Tunnel to expose the broker to the public internet, and managing access and routing through Cloudflare's network.
-
Go to the Cloudflare Dashboard and log in with your credentials.
-
In the left sidebar, click on "Zero Trust".
-
Under the "Networks" section in the sidebar, select "Tunnels".
-
Click on "Create a tunnel".
-
On the next screen, select "Cloudflared" as the tunnel type and click "Next".
-
Enter a name for your tunnel, such as
my_tunnel_name
, and click "Save tunnel". -
Choose "Docker" as your environment. Copy the provided Docker command which includes your unique token.
Create an .env
file in the base directory of this repo, and add CLOUDFLARE_TUNNEL_TOKEN=your_token
. See .env.sample for example.
Note: You do not need to run the provided Docker command on the screen. This repository replaces that step.
-
After running the connector, navigate to the "Tunnels" section again and select the newly created tunnel.
-
Click on "Next" to proceed to the hostname configuration.
-
On the "Public Hostname Page", fill in the required details:
- Public hostname: Choose your desired subdomain and domain.
- Service: Select the service type (HTTP).
- URL: Set the URL to point to your Mosquitto service (e.g.,
mosquitto:9001
). Docker will route themosquitto
hostname to the relevantcontainer_name
in thedocker-compose.yml
.
-
Click "Save hostname" to finalize the configuration.
docker compose up