Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

This documentation is part of the "Projects with Books" initiative at zenOSmosis.

The source code for this project is available on GitHub.

Getting Started

Loading…

Getting Started

Relevant source files

This page guides you through the complete deployment process for the Docker MQTT Mosquitto Cloudflare Tunnel system. The setup consists of four phases: verifying prerequisites, configuring Cloudflare Tunnel, setting up local configuration files, and deploying containers.

Related Pages:

  • Prerequisites (page 2.1): System requirements and account setup
  • Cloudflare Tunnel Setup (page 2.2): Detailed tunnel creation steps
  • Local Configuration (page 2.3): Environment and configuration file setup
  • Deployment (page 2.4): Container deployment and verification

Deployment Workflow

The deployment process follows a linear sequence of configuration and initialization steps:

Diagram: Deployment Phase Sequence

stateDiagram-v2
    [*] --> Prerequisites
    Prerequisites --> CloudflareTunnel : Accounts verified
    CloudflareTunnel --> LocalConfig : CLOUDFLARE_TUNNEL_TOKEN obtained
    LocalConfig --> Deployment : .env file created
    Deployment --> [*] : Containers running
    
    state CloudflareTunnel {
        [*] --> CreateTunnel
        CreateTunnel --> ConfigureHostname
        ConfigureHostname --> [*]
    }
    
    state LocalConfig {
        [*] --> CreateEnvFile
        CreateEnvFile --> PopulateToken
        PopulateToken --> [*]
    }
    
    state Deployment {
        [*] --> DockerComposeUp
        DockerComposeUp --> VerifyContainers
        VerifyContainers --> [*]
    }

Sources : README.md:29-78

Deployment Phases Summary

PhaseKey ActionsOutput ArtifactsPage Reference
PrerequisitesInstall Docker, Docker Compose; create Cloudflare accountSystem readinessPage 2.1
Cloudflare Tunnel SetupCreate tunnel in Zero Trust dashboard; configure public hostnameCLOUDFLARE_TUNNEL_TOKENPage 2.2
Local ConfigurationCreate .env file; populate token; verify mosquitto.conf.env filePage 2.3
DeploymentExecute docker compose up; verify container statusRunning mosquitto and cloudflared containersPage 2.4

Sources : README.md:29-78 docker-compose.yml:1-18

Configuration File Flow

Diagram: File-to-Container Mapping

Sources : docker-compose.yml:1-18 .env.sample:1-3 .gitignore:1-2

Configuration File Purposes

FileVersion ControlledPurposeUsed By
docker-compose.ymlYesDefines service orchestration, container names, image references, volume mounts, environment variable injectionDocker Compose
mosquitto.confYesConfigures MQTT broker listeners on ports 1883 and 9001mosquitto container
.env.sampleYesTemplate showing required environment variablesDevelopers (documentation)
.envNoContains actual CLOUDFLARE_TUNNEL_TOKEN valuecloudflared container
.gitignoreYesPrevents .env from being committedGit

Sources : docker-compose.yml:1-18 mosquitto.conf:1-5 .env.sample:1-3 .gitignore:1-2

Quick Start Summary

This section provides an abbreviated setup sequence. For detailed instructions, see the child pages (2.1 through 2.4).

Step 1: Cloudflare Tunnel Configuration

Create a tunnel in the Cloudflare Zero Trust dashboard and obtain the CLOUDFLARE_TUNNEL_TOKEN. Configure a public hostname that routes to mosquitto:9001.

Detailed instructions : See page 2.2 (Cloudflare Tunnel Setup)

Key outputs :

  • Tunnel token string (begins with eyJ...)
  • Public hostname (e.g., mqtt.example.com)

Sources : README.md:29-73

Step 2: Local Environment Setup

Create .env file from template and populate with tunnel token:

Edit .env:

CLOUDFLARE_TUNNEL_TOKEN=<token_from_step_1>

Detailed instructions : See page 2.3 (Local Configuration)

File locations :

Sources : .env.sample:1-3 README.md53

Step 3: Container Deployment

Execute Docker Compose to start both services:

Detailed instructions : See page 2.4 (Deployment)

Services started :

Sources : README.md:76-78 docker-compose.yml:1-18

Container Initialization

Diagram: Docker Compose Service Startup Sequence

Sources : docker-compose.yml:1-18 mosquitto.conf:1-5

Post-Deployment Verification

Verify both containers are operational:

Expected output shows two running containers:

Container NameImageStatusPorts
mosquittoeclipse-mosquitto:latestUp1883/tcp, 9001/tcp
cloudflaredcloudflare/cloudflared:latestUp(no exposed ports)

Check cloudflared logs for tunnel establishment confirmation:

Check mosquitto logs for listener initialization:

Sources : docker-compose.yml:4-9 docker-compose.yml:11-17

Docker Compose Service Configuration

Diagram: Service Definition to Runtime Mapping

Sources : docker-compose.yml:1-18

Service Configuration Parameters

mosquitto Service

ParameterValuePurpose
container_namemosquittoDNS-resolvable name for internal routing
imageeclipse-mosquitto:latestOfficial Mosquitto MQTT broker image
restartunless-stoppedAutomatic restart unless explicitly stopped
volumes./mosquitto.conf:/mosquitto/config/mosquitto.confInject broker configuration

File reference : docker-compose.yml:4-9

cloudflared Service

ParameterValuePurpose
container_namecloudflaredContainer identifier for logging and management
imagecloudflare/cloudflared:latestOfficial Cloudflare Tunnel connector image
restartunless-stoppedAutomatic restart unless explicitly stopped
commandtunnel --no-autoupdate runExecute tunnel with auto-update disabled
environmentCLOUDFLARE_TUNNEL_TOKEN from .envAuthenticates tunnel connection

File reference : docker-compose.yml:11-17

Sources : docker-compose.yml:1-18

Connection Flow After Deployment

Once both containers are operational, the request flow follows this path:

  1. External clients connect to public hostname (e.g., https://mqtt.example.com:443)
  2. Cloudflare edge network receives connection
  3. Traffic routes through tunnel to cloudflared container
  4. cloudflared proxies to mosquitto:9001 via Docker internal DNS
  5. mosquitto container handles connection on WebSocket listener (port 9001)

Diagram: Client-to-Broker Request Path

graph LR
    Client["MQTT Client"]
CFEdge["Cloudflare Edge\n(Port 443)"]
Tunnel["Tunnel Infrastructure"]
CFDContainer["cloudflared container"]
MosqContainer["mosquitto container\n(Port 9001)"]
Client -->|HTTPS/WSS| CFEdge
 
   CFEdge -->|Tunnel protocol| Tunnel
 
   Tunnel -->|Encrypted tunnel| CFDContainer
 
   CFDContainer -->|HTTP to mosquitto:9001| MosqContainer

Sources : README.md:64-67 README.md82

System Management Commands

CommandActionEffect
docker compose upStart services in foregroundContainers run, logs visible in terminal
docker compose up -dStart services in backgroundContainers run detached from terminal
docker compose downStop and remove containersContainers stopped, network removed, volumes persist
docker compose restartRestart both servicesContainers restarted with existing configuration
docker logs mosquittoView mosquitto container logsDisplay broker initialization and connection logs
docker logs cloudflaredView cloudflared container logsDisplay tunnel establishment and proxy logs

Sources : docker-compose.yml:1-18

Next Steps

For detailed setup instructions , refer to child pages:

PageTitleContent
2.1PrerequisitesSystem requirements, Docker installation, Cloudflare account setup
2.2Cloudflare Tunnel SetupStep-by-step tunnel creation, hostname configuration, token extraction
2.3Local Configuration.env file creation, mosquitto.conf customization, security validation
2.4DeploymentContainer startup, health verification, troubleshooting common errors

For advanced topics , see:

  • Component details : Pages 3.1 (Mosquitto), 3.2 (Cloudflared), 3.3 (Docker Compose)
  • Configuration reference : Pages 4.1, 4.2, 4.3
  • Production deployment : Page 6.2
  • Advanced security features : Page 6.1 (see protected-no-wildcard branch for ACL and encryption)

Sources : README.md:7-13 (protected branch reference)

Common Initial Setup Issues

IssueSymptomResolution
Missing .env filecloudflared fails to start with authentication errorCreate .env file from .env.sample:1-3 template and add valid token
Invalid tunnel tokencloudflared logs show authentication failureVerify token in Cloudflare dashboard and update .env file
Port conflictsContainer fails to start with port binding errorCheck if ports 1883 or 9001 are already in use on the host
Tunnel not foundcloudflared reports tunnel does not existEnsure tunnel was created in Cloudflare dashboard and token matches
Public hostname not configuredClients cannot connectConfigure public hostname in Cloudflare dashboard as described in Step 2
Volume mount errorsmosquitto fails to load configurationVerify mosquitto.conf exists in repository root as defined in docker-compose.yml8

For comprehensive troubleshooting, see Troubleshooting.

Sources : README.md:23-73 docker-compose.yml:1-18 .env.sample:1-3

Dismiss

Refresh this wiki

Enter email to refresh