gpbenton / engMQTTClient

MQTT client for Energenie ener314-rt board running on a raspberry Pi
MIT License
21 stars 10 forks source link

docker-compose assumes 192.168.0.3 for mosquito broker. #31

Closed DrHoneyBear closed 3 years ago

DrHoneyBear commented 3 years ago

Currently, your energenie_mqtt_service includes the directive:- command: ["-h", "192.168.0.3"]

This will force mqtt clients to use 192.168.0.3. Mine runs on localhost so this would not work.

Recommend that users specify the broker host as an argument passed to docker-compose command. where docker-compose.yaml is changed to the following:-

services:
  energenie_mqtt_client:
    image: gpbenton/engmqttclient:1.0.0
    restart: unless-stopped
    container_name: energenie_mqtt_client
    privileged: true
    working_dir: /opt/energenie_mqtt_client/
    network_mode: host
    environment:
      - "MQTT_BROKER=${MQTT_BROKER}"
    command: ["-h", "${MQTT_BROKER}"]
    #entrypoint: /bin/sh -c 'LD_LIBRARY_PATH=/usr/local/lib ./engMQTTClient.exe 2>&1'

  # Remove if you already have mosquitto installed elsewhere
  mosquitto:
    container_name: mosquitto
    restart: unless-stopped
    image: eclipse-mosquitto
    container_name: mosquitto
    volumes:
      - ./mosquitto_config:/mosquitto/config:ro
      - mosquitto_persistence_database:/mosquitto/data
      - /etc/localtime:/etc/localtime:ro
    #user: "1883:1883"
    ports:
      - "1883:1883"
      - "8883:8883"
      - "9001:9001"

volumes:
  mosquitto_persistence_database:

Then, the command to spin up the container would be:- sudo MQTT_BROKER=localhost docker-compose up -d

This assumes the environment variable MQTT_BROKER is used and of course, localhost is the broker IP - but this can be changed obviously.

Tested and working on Ubuntu and Raspbian.

And for completeness, here is the updated engmqtt.service unit file - of course this is set for localhost and runs in the /home/daz/DOCKER/ENERGENIE directory so that would need to change. Ideally these should be put in /etc/sysconfig or something as env vars and referenced in the unit file .

[Service]
Description=Energenie MQTT Client service
Requires=docker.service
After=docker.service
Environment=MQTT_BROKER=localhost
WorkingDirectory=/home/daz/DOCKER/ENERGENIE
ExecStart=/usr/bin/docker-compose -f /home/daz/DOCKER/ENERGENIE/docker-compose.yaml up
ExecStop=/usr/bin/docker-compose -f  /home/daz/DOCKER/ENERGENIE/docker-compose.yaml down
[Install]
WantedBy=multi-user.target
gpbenton commented 3 years ago

I regard the docker-compose as a config file which anyone will edit as they wish. For instance, most users will have a MQTT broker working already, so will probably remove that section.

In your case, you will also change the appropriate address. If you want to do this with an environmental variable, that's fine, but it seems more error prone than actually putting the value in the file. You are going to have to ensure that the correct value is set in you environment whenever you start the program.

The only reason I added this docker-compose file at all was because the original author of the docker image included the mosquitto broker inside the image, and I needed to show him how to achieve the same thing with separate docker images.

I'm don't see the need to for a systemd service. Docker restarts its services on reboot anyway.

gpbenton commented 3 years ago

I have moved this example to the wiki page, which seems more appropriate.