Mosquitto on Docker
What is Mosquitto?
Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for "Internet of Things" messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino.
Yet another Mosquitto container?
- Based on the official Alpine Linux image - super slim and lightweight.
- No magic. Straight config that follows upstream. Simple and clean configuration via environment variables or config file.
- Image follows Dockerfile best practices (dropping root privileges, PID1 for proper signalling, logging,...)
- All user data under
/data- ready-to mount.
Start Mosquitto and name the container
$ docker run -d \ --name mosquitto_test \ machine-data/mosquitto
Push a message to the broker:
$ docker run \ --rm -ti \ --link mosquitto_test \ machine-data/mosquitto \ mosquitto_pub \ -h mosquitto_test \ -t "mytopic/mypath" \ -m "message payload" \ -q 1 -r
Consume one message from the broker:
$ docker run \ --rm -ti \ --link mosquitto_test:mosquitto \ machine-data/mosquitto \ mosquitto_sub \ -h localhost \ -t "mytopic/mypath" \ -C 1
If Mosquitto ends, it will save the in-memory database to the
/data directory, therefore it is recommended to bind mount a volume.
$ mkdir data $ docker run -d \ --volume $(pwd)/data:/data \ --publish 1880:1880 \ machine-data/mosquitto
It is very easy to configure Mosquitto via environment variables. If no config file is present, the
docker-entrypoint.sh script will create one based on sane defaults and environment variables.
MOSQUITTO_LOG_DEST: Change this from the default
stdoutif you like the logs being written to disk.
MOSQUITTO_PERSISTENCE_LOCATION: The directory where the in-memory database is being saved. This translates to
mosquitto.conf. In most cases it makes sense to keep the
The container is configured to start Mosquitto with
/config/mosquitto.conf as config file.
If a config file is mounted (preferably read-only), the
MOSQUITTO_ environment variables will be ignored:
$ curl -O https://raw.githubusercontent.com/eclipse/mosquitto/master/mosquitto.conf $ sed -e "s#log_dest syslog#log_dest stdout#" \ -e "s#\#persistence_location#persistence_location /data#" \ $ docker run -d \ -v $(pwd)/mosquitto.conf:/config/mosquitto.conf:ro \ -p 1883:1883 machine-data/mosquitto
/data: Path where Mosquitto's in-memory database id being saved.
1883: Port for the default listener
8883: Recommended port for MQTT over TLS, needs to be set manually
9001: Websocket listener port, needs to be set manually
Mosquitto was written by Roger Light email@example.com and is dual licensed under the Eclipse Public License 1.0 and the Eclipse Distribution License 1.0.
Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
If you have any questions don't hesitate to join us on Slack.