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条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况