简体   繁体   中英

How to make a TCP outgoing connection with Docker container?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM