英文:
How to make a TCP outgoing connection with Docker container?
问题
我的Go应用程序通过tls.Dial()
建立TLS连接来交换数据。
当从主机上运行时,它可以正常工作:
但是,当应用程序从Docker容器中运行时,出站连接似乎无法工作。应用程序会无限期地挂起。
注意1:使用docker run -p $(docker-machine ip):2500:2500 ...
时出现相同的行为。
注意2:VM除了docker-machine的默认VM附带的默认设置之外,没有额外的端口转发设置。
使用Dockerfile构建Docker镜像:
FROM golang:latest
RUN mkdir -p "$GOPATH/src/path/to/app"
# 安装依赖
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"]
主机是运行_docker-machine_的OS X。
问题:
如何使TCP出站连接正常工作?
英文:
My Go application makes TLS connections via tls.Dial()
to exchange data.
It works fine when run from the host:
But the outgoing connection doesn't seem to work when the app is run from a Docker container. The app hangs indefinitely.
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?
答案1
得分: 5
你要么使用boot2docker,要么使用docker-machine(因为你在OSX上运行docker)。如果你使用的是boot2docker,你需要在VirtualBox上和docker上都转发端口,请参考这篇博客文章:https://fogstack.wordpress.com/2014/02/09/docker-on-osx-port-forwarding/
如果你使用的是docker-machine,你需要连接到docker-machine分配的IP地址,而不是localhost,请参考这篇文章:https://github.com/docker/machine/issues/710
我现在看到你特别使用的是docker-machine,所以关于docker-machine的文章应该能回答你的问题。
编辑:我误解了问题。你正在尝试在转发的端口上进行出站连接,这是不正确的。默认情况下,docker可以在任何端口进行出站连接。端口转发只用于入站连接。请再次尝试不指定任何要转发的端口。我怀疑你正在尝试在入站(转发)端口上进行出站连接。
英文:
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.
答案2
得分: 1
我刚刚遇到了完全相同的问题。无法进行任何外部连接。
重新启动容器后,突然外部连接正常工作了。可能是容器在 Docker 的更新中幸存下来了?
目前使用的 Docker 版本是 18.09.3,构建版本是 774a1f4。
英文:
I've just had exactly the same problem. Was unable to connect out at all.
Restarted the container, and suddenly outgoing connections worked fine. It's possible that the container survived an update of docker?
Currently using Docker version 18.09.3, build 774a1f4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论