ds19891231 2016-03-04 04:55
浏览 510
已采纳

如何与Docker容器建立TCP传出连接?

My Go application makes TLS connections via tls.Dial() to exchange data.

It works fine when run from the host:

enter image description here

But the outgoing connection doesn't seem to work when the app is run from a Docker container. The app hangs indefinitely.

enter image description here

Note 1: Same behavior with using docker run -p $(docker-machine ip):2500:2500 ...

Note 2: VM doesn't have extra port forwarding settings other than the default settings that came with docker-machine's default VM.

Docker image build with Dockerfile:

FROM golang:latest

RUN mkdir -p "$GOPATH/src/path/to/app"
# Install dependencies
RUN go get github.com/path/to/dep

VOLUME "$GOPATH/src/path/to/app"
EXPOSE 2500
WORKDIR "$GOPATH/src/path/to/app"
CMD ["go", "run", "main.go"]

Host is OS X running docker-machine.

Question

How can I make the TCP outgoing connection to work?

  • 写回答

2条回答 默认 最新

  • douzhao7634 2016-03-04 05:14
    关注

    You are either using boot2docker or docker-machine (since you are running docker on OSX). If you are using boot2docker, you have to forward the ports on VirtualBox as well as docker, have a look at this blog post: https://fogstack.wordpress.com/2014/02/09/docker-on-osx-port-forwarding/

    If you are using docker-machine, you have to connect to the docker-machine assigned ip, not localhost, have a look at this post: https://github.com/docker/machine/issues/710

    I see now that you are using docker-machine specifically, so the post about docker-machine should answer your question.

    Edit: I misunderstood the question. You are trying to make an outgoing connection on a forwarded port. That is not correct. By default docker can make outgoing connections on any port. The port forwarding is for incoming connections only. Please try again without specifying any ports to forward. My suspicion is that you are trying to make an outgoing connection on the incoming (forwarded) port.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error