简体   繁体   English

无法与 docker 内部的 mosquitto MQTT broker 连接

[英]Can not connect with mosquitto MQTT brocker inside docker

I have a mosquitto mqtt brocker running in docker.我有一个 mosquitto mqtt broker 在 docker 中运行。 I am starting it is a docker compose file.我开始它是一个 docker 组合文件。 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.当我尝试作为 docker 容器连接时,尽管我已将主机/代理地址从本地更改为组合图像名称,但它无法正常工作。 How can I make it work?我怎样才能让它工作?

Here What I have tried Docker compose ( edited )在这里我尝试过 Docker 撰写(已编辑)

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和我的 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这是conf文件

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

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) eg network called foo_default当使用 docker compose 运行时,默认情况下作为服务启动的容器被放置在以项目命名的专用 docker 覆盖网络上(默认为目录名称docker-compose.yml保存在例如网络中foo_default

https://docs.docker.com/compose/networking/ 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.因此,如果您在docker-compose.yml中只有 mosquitto,那么其他容器将无法连接到它。 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.如果您在撰写文件中包含 python 作为服务运行的容器,那么它将能够连接。

You can also change the networks containers connect to in the compose file.您还可以在 compose 文件中更改连接到的网络容器。

https://docs.docker.com/compose/networking/#specify-custom-networks 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 .您已强制 mosquitto 服务使用network_mode: host network 所以它与app不在同一个postgres网络上。 Containers can be on multiple networks, but mosquitto should not be bound to the host network to make all this work.容器可以位于多个网络上,但 mosquitto 不应绑定到主机网络以使所有这些工作。

EDIT2:编辑2:

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.即使您在mosquitto.conf中要求身份验证并且您将密码文件指向配置文件,您也没有在应用程序中设置用户名/密码,这将不起作用。 I suggest you remove the last line of the mosquitto.conf file and set allow_anonymous true .我建议你删除 mosquitto.conf 文件的最后一行并设置mosquitto.conf allow_anonymous true

Ps I suspect that the mosquitto container currently isn't actually starting due to the last line of the config file. Ps 我怀疑由于配置文件的最后一行,mosquitto 容器当前实际上并没有启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM