无法连接到docker中运行的tcp端口

时间:2018-02-17 10:33:07

标签: scala docker sbt akka

我使用sbt和Akka编写了一个mqqt代理。我在本地计算机上运行正常,但是当我在docker容器中运行代码时,我无法连接到代理。我看到代理在端口1883上正常运行。 Docker日志: -

[DEBUG] [02/17/2018 04:24:29.682] [run-main-0] [EventStream(akka://TcpServer)] Default Loggers started
[DEBUG] [02/17/2018 04:24:29.853] [TcpServer-akka.actor.default-dispatcher-4] [akka://TcpServer/system/IO-TCP/selectors/$a/0] Successfully bound to /127.0.0.1:1883
[INFO] [02/17/2018 04:24:29.860] [TcpServer-akka.actor.default-dispatcher-4] [akka://TcpServer/user/tcp] listening on port 1883

我的dockerfile: -

FROM openjdk:8
ENV SBT_VERSION 0.13.16
RUN curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && dpkg -i sbt-$SBT_VERSION.deb && rm sbt-$SBT_VERSION.deb &&  apt-get update && apt-get install sbt && sbt sbtVersion

WORKDIR /app
ADD . /app
EXPOSE 1883
CMD sbt run

Docker运行命令

docker run -v "$HOME/.ivy2":/root/.ivy2 -p 1883:1883 messanger:v1

docker container ls

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6a0a6aee317f        messanger:v1        "/bin/sh -c 'sbt run'"   6 hours ago         Up 6 hours          0.0.0.0:1883->1883/tcp   epic_goldberg

我可以通过登录到docker机器而不是从我的本地机器连接到代理。在docker中运行mqtt客户端日志

root@e14df5e36def:/# python mqtt_subscriber.py
Connected with result code 0
<paho.mqtt.client.Client object at 0x7fe5a66cb510> None 1

1 个答案:

答案 0 :(得分:0)

从日志中,您可以看到您的应用绑定到环回接口:

[DEBUG] [02/17/2018 04:24:29.682] [run-main-0] [EventStream(akka://TcpServer)] Default Loggers started [DEBUG] [02/17/2018 04:24:29.853] 
[TcpServer-akka.actor.default-dispatcher-4] [akka://TcpServer/system/IO-TCP/selectors/$a/0] Successfully bound to /127.0.0.1:1883 [INFO] [02/17/2018 04:24:29.860] 
[TcpServer-akka.actor.default-dispatcher-4] [akka://TcpServer/user/tcp] listening on port 1883

您的应用需要绑定到接口0.0.0.0,而不是127.0.0.1。容器有自己的命名空间环回接口,即docker端口转发无法访问。