简体   繁体   English

从一个 docker 容器连接到另一个 [相同网络] 时连接被拒绝

[英]Connection refused when connecting from one docker container to another [same network]

Having a hard time debugging this.很难调试这个。 I have one container starting an MQTT server and another Python container trying to connect.我有一个容器启动 MQTT 服务器,而另一个 Python 容器试图连接。

Using docker-compose to orchestrate and have set up a network and connected both containers to it.使用 docker-compose 编排并建立了一个网络并将两个容器连接到它。

Currently I can connect to the MQTT server independently with docker run and with docker-compose, but the Python containe for some reason cannot connect.目前我可以通过运行 docker 和 docker-compose 独立连接到 MQTT 服务器,但 Python 包含由于某种原因无法连接。

I'm thinking it may be a firewall issue?我在想这可能是防火墙问题?

In the main.py I'm printing the MQTT_HOST and MQTT_PORT and I can actually connect to those using a local Mosquitto client.在 main.py 中,我正在打印 MQTT_HOST 和 MQTT_PORT,我实际上可以使用本地 Mosquitto 客户端连接到那些。

docker-compose.yml docker-compose.yml

version: "3.3"
services:
  webserver:
    build: ./webservice/server
    ports:
      - 3001:3001
      - 3002:3002
    networks:
      - project-network
    command: npm run start
  inferemce:
    build: ./inference
    ports:
      - 3003:3003
    networks:
      - project-network
    depends_on:
      - webserver
    restart: on-failure
    command: ["./wait-for-it.sh", "webserver:3001", "--", "python", "main.py"]

networks:
  project-network:
    driver: bridge

main.py主文件

import socket
import paho.mqtt.client as mqtt

HOSTNAME = socket.gethostname()
IPADDRESS = socket.gethostbyname('localhost')
MQTT_HOST = IPADDRESS
MQTT_PORT = 3001
MQTT_KEEPALIVE_INTERVAL = 60

def connect_mqtt():
    print('host', MQTT_HOST)
    print('port', MQTT_PORT)
    client = mqtt.Client()
    client.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
    return client

def main():
    client = connect_mqtt()



if __name__ == '__main__':
    main()

You are trying to connect to localhost which is going to be the container the python app is running in, not webserver .您正在尝试连接到localhost ,它将成为 python 应用程序正在运行的容器,而不是webserver

Change the python to try to look up the address of webserver rather than localhost更改 python 以尝试查找webserver的地址而不是localhost

import socket
import paho.mqtt.client as mqtt

HOSTNAME = socket.gethostname()
IPADDRESS = socket.gethostbyname('webserver')
MQTT_HOST = IPADDRESS
MQTT_PORT = 3001
MQTT_KEEPALIVE_INTERVAL = 60
...

暂无
暂无

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

相关问题 通过 Flask Z05B6053C41A2130AFD6FC3B158B 容器连接时,连接被 postgresql docker 容器拒绝 - Connection refused by postgresql docker container when connecting via Flask docker container urlopen错误[Errno 111]从一个虚拟机连接到另一个虚拟机时,python中的连接被拒绝 - urlopen error [Errno 111] Connection refused in python when connecting from one VM to another 同一服务器中的 docker 容器拒绝 PostgresSQL 连接 - PostgresSQL connection refused on docker container in same server MongoDB Docker Flask ZC5FD214CDD0D2B3B42272E73B022BA 容器上的连接被拒绝 - Connection Refused on MongoDB Docker Container from Flask Docker Container Docker:当使用容器名称从一个容器连接到另一个容器时,如何修复“无法将主机名”postgres“转换为地址”? - Docker: How to fix “could not translate host name ”postgres“ to address” when connecting from one container to another using container name? 从另一个容器中的服务连接到rabbitmq docker容器 - Connecting to rabbitmq docker container from service in another container docker 容器中的 supervisor.sock 拒绝连接 - supervisor.sock refused connection in docker container 使用简单的python tcp脚本连接同一台主机上的两个Docker容器,但出现连接被拒绝的错误 - Use a simple python tcp script to connect two Docker container on the same host machine but got Connection refused error 远程连接到 Elasticsearch 时连接被拒绝 - Connection Refused When Connecting to Elasticsearch Remotely 使用另一个容器连接到 MongoDB docker 容器的问题 - Issue with connecting to MongoDB docker container with another container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM