-1

I have a mosquitto mqtt brocker running in docker. I am starting it is a docker compose file. Now I am trying to connect with the broker, it was working locally. When I try to connect as a docker container it is not working although I have changed the host/brocker address from local to compose image name. How can I make it work ?

Here What I have tried Docker compose ( edited )

version: '3.5'

services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
       - pgdatapg:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped

  mosquitto:
    image: eclipse-mosquitto
    networks:
      - postgres
    ports:
      - "1883:1883"
    volumes:
      - ./conf:/mosquitto/conf
      - ./data:/mosquitto/data
      - ./log:/mosquitto/log

  app:
    restart: always
    build: .
    depends_on:
      - db
    networks:
      - postgres

networks:
  postgres:
    driver: bridge

volumes:
    pgdatapg:

and part of my python

broker = "mosquitto"
port = 1883
topic = "py/mqtt/test"
def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

Here is the conf file

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

listener 1883
## Authentication ##
allow_anonymous false
password_file /mosquitto/conf/mosquitto.conf

I am getting the following error

| ConnectionRefusedError: [Errno 111] Connection refused

1 Answers1

0

When running with docker compose, the containers started as services by default are placed on dedicated docker overlay network named after the project (which defaults to the dir name the docker-compose.yml file is held in) e.g. network called foo_default

https://docs.docker.com/compose/networking/

Services are only accessible from other containers that are connected to the same network (and to the host via what ever ports are exposed).

So if you only have mosquitto in the docker-compose.yml then no other containers will be able to connect to it. If you include the container that the python is running in as a service in the compose file then it will be able to connect.

You can also change the networks containers connect to in the compose file.

https://docs.docker.com/compose/networking/#specify-custom-networks

EDIT:

You have forced the mosquitto service to use network_mode: host network so it's not on the same postgres network as the app. Containers can be on multiple networks, but mosquitto should not be bound to the host network to make all this work.

EDIT2:

You are also not setting a username/password in app even though you have require authentication in mosquitto.conf and you are pointing the password file at the config file which just won't work. I suggest you remove the last line of the mosquitto.conf file and set allow_anonymous true.

P.s. I suspect that the mosquitto container currently isn't actually starting due to the last line of the config file.

hardillb
  • 54,545
  • 11
  • 67
  • 105