简体   繁体   English

Docker-Compose使用python连接到远程MQTT代理

[英]Docker-Compose connect to distant MQTT broker with python

Totally new to Docker-Compose and I kind of breaking my face repeatedly. 对于Docker-Compose来说是全新的,我有点反复地断脸。 I try to connect to an existing mqtt broker (my broker on another pc) over docker-compose, with a test python script that is working standalone. 我尝试使用独立运行的测试python脚本通过docker-compose连接到现有的mqtt代理(另一台PC上的代理)。

This is the Python testscript. 这是Python测试脚本。 I also tried to print in Terminal but with no luck. 我也尝试在终端机中打印,但是没有运气。 So help is appreciated on this one too. 因此,在这一方面也有所帮助。

import json
import base64
import binascii
import time
import datetime
from collections import defaultdict
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import ssl

def on_message(mosq, obj, msg):
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

def on_connect(client, userdata, a, b):
    print("Connected to Broker")




def on_disconnect(client, userdata, rc):
    print("Disconnected from MQTT server with code: ", rc)


mqttc = mqtt.Client(client_id="RELAY", clean_session=False)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
mqttc.connect("1.1.1.2", 1883, 60)

mqttc.loop_start()

counter = 0 
while True:
    print("Sending MSG")
    counter=counter+1
    response="MESSAGE No: "+ str(counter) 
    mqttc.publish("Walrus/test", response)

    time.sleep(30)

This is one of the thousands instances of the yml file I tried. 这是我尝试的yml文件的数千个实例之一。

version: '3'
services:
  mqtt:
     broker: 1.1.1.2
     port:1883

This instance just returns that broker is unsupported config option 该实例仅返回不支持代理的配置选项

This is the Dockerfile 这是Dockerfile

FROM python:3.6.1-alpine

ADD . /pyapp
WORKDIR /pyapp
ENV PATH=/home/ubuntu/.virtualenvs/bin:$PATH
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install -r requirements.txt

EXPOSE 1883

CMD ['python', '-u', 'app.py']

Any tutorial I 've seen is creating a broker or uses a broker as a service/package. 我见过的任何教程都是创建代理或将代理用作服务/程序包。 I think what I want to do is much simpler but I haven't found a tutorial. 我认为我想做的事情要简单得多,但是我还没有找到教程。 I know that I am missing something basic. 我知道我缺少一些基本知识。 Any help is appreciated. 任何帮助表示赞赏。

You can not specify the external broker in the docker-compose file, it's for specifying services you are creating with docker. 您不能在docker-compose文件中指定外部代理,这是用于指定使用docker创建的服务。

The best you could do is include the details of the remote broker as environment variables to be passed to your script. 您能做的最好的事情就是将远程代理的详细信息作为要传递给脚本的环境变量包括在内。

Also there is no point to the EXPOSE 1883 line for your script container because it is a client not a "server". 同样,指向脚本容器的EXPOSE 1883行也没有意义,因为它是客户端而不是“服务器”。

As mentioned in the comments, you should follow the tutorial fully in order to get a good grounding in how to build composite services with docker-compose before you try and do something different. 如评论中所述,您应该完全按照本教程进行操作,以便在尝试执行其他操作之前获得如何使用docker-compose构建复合服务的良好基础。

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

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