diff --git a/.mqtt/mosquitto.conf b/.mqtt/mosquitto.conf index 7056a9b..5d07edf 100644 --- a/.mqtt/mosquitto.conf +++ b/.mqtt/mosquitto.conf @@ -235,7 +235,7 @@ listener 1883 listener 9001 #socket_domain ipv4 protocol websockets -allow_anonymous true +#allow_anonymous true # By default, a listener will attempt to listen on all supported IP protocol # versions. If you do not have an IPv4 or IPv6 interface you may wish to diff --git a/Dockerfile b/Dockerfile index fdf4083..f8d171b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,8 @@ FROM eclipse-mosquitto +RUN apk add --no-cache mosquitto-clients +COPY entrypoint.sh /entrypoint.sh COPY .mqtt/mosquitto.conf /mosquitto/config/mosquitto.conf +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] EXPOSE 1883 EXPOSE 9001 diff --git a/README.md b/README.md index 4e0c3ed..cf8cc9d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## English ### Description -MQTT Server pre configured with default options to run 'plug and play' +MQTT Server pre configured with default options to run 'plug and play'. User and password can be set with `USER` and `PASSWORD` parameters. ### How to use @@ -13,11 +13,15 @@ Just run from a terminal: ```sh docker run -d -p 1883:1883 -p 9001:9001 --name mosquitto ggfto/mosquitto:latest ``` +With user and password: +```sh +docker run -d -p 1883:1883 -p 9001:9001 -e USER=myuser -e PASSWORD=mypassword --name mosquitto ggfto/mosquitto:latest +``` ## Português ### Descrição -Servidor MQTT pré-configurado com opções padrão para rodar sem a necessidade de ajustar algo +Servidor MQTT pré-configurado com opções padrão para rodar sem a necessidade de ajustar algo. Usuário e senha podem ser configurados com os parâmetros `USER` e `PASSWORD` ### Como usar @@ -25,3 +29,7 @@ Apenas execute a partir de um terminal: ```sh docker run -d -p 1883:1883 -p 9001:9001 --name mosquitto ggfto/mosquitto:latest ``` +Com usuário e senha: +```sh +docker run -d -p 1883:1883 -p 9001:9001 -e USER=myuser -e PASSWORD=mypassword --name mosquitto ggfto/mosquitto:latest +``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..c8b42de --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,13 @@ +#!/bin/sh +if [ ! -f /mosquitto/config/pwfile ]; then + if [ -n "$USER" ] && [ -n "$PASSWORD" ]; then + mosquitto_passwd -b -c /mosquitto/config/pwfile "$USER" "$PASSWORD" + chmod 700 /mosquitto/config/pwfile + chown mosquitto:mosquitto /mosquitto/config/pwfile + echo "password_file /mosquitto/config/pwfile" >> /mosquitto/config/mosquitto.conf + echo "allow_anonymous false" >> /mosquitto/config/mosquitto.conf + else + echo "allow_anonymous true" >> /mosquitto/config/mosquitto.conf + fi +fi +exec mosquitto -c /mosquitto/config/mosquitto.conf \ No newline at end of file