DEV Community

Cover image for A basic MQTT Docker deployment
J Fowler
J Fowler

Posted on

A basic MQTT Docker deployment

In this post, I show how to setup a basic MQTT broker using the Eclipse Mosquitto MQTT broker using Docker.

I'll be referencing this in future posts as I build out a system for monitoring IoT devices using Go and a number of other open-source technologies.

Let's start with the Dockerfile

# Use the official Eclipse Mosquitto image as the base
FROM eclipse-mosquitto:latest

# Set the maintainer label
LABEL maintainer="your-name <you@example.com>"

# Expose MQTT port
EXPOSE 1883

# Expose MQTT over WebSockets port (if needed)
EXPOSE 9001

# Copy custom configuration file (if you have one)
COPY mosquitto.conf /mosquitto/config/mosquitto.conf

# Set the entrypoint to run Mosquitto
ENTRYPOINT ["/usr/sbin/mosquitto", "-c", "/mosquitto/config/mosquitto.conf"]
Enter fullscreen mode Exit fullscreen mode

this is pretty straightforward. It retrieves the latest distribution, exposes the ports used by clients, copies a config file, then declares how to run it.

What about the config file? Here it is and it is pretty basic as well.

# Allow anonymous connections (no authentication)
allow_anonymous true

# Listen on all network interfaces
listener 1883 0.0.0.0

# Enable WebSocket support (optional)
listener 9001
protocol websockets

# Logging configuration
log_dest stdout
log_type all

# Persistence
persistence true
persistence_location /mosquitto/data/

# Uncomment to enable more verbose logging
# log_type all

# Maximum number of client connections
# -1 means no limit
max_connections -1

# Connection timeout in seconds
connection_messages true
Enter fullscreen mode Exit fullscreen mode

Note that this configuration does not include any authentication. You wouldn't want to do that in a production system, but is fine for this example.

The broker is set to listen on all interfaces. This is important as it would only listen on localhost otherwise.

We have persistence enabled to add some durability to the deployment.

Another setting of note is max_connections. With it set to -1, there is no limit. In a production system, you may want to limit concurrent connections performance reasons.

Check the Mosquitto documentation here for a full description of all the options available. It is pretty flexible and makes it suitable for many commercial uses.

In the next post, I provide a simple Go application to push messages to the broker.

The full Dockerfile and configuration is open source and available here

Let me know your thoughts in the comments.

Thanks!

Top comments (0)