Beta 15: Port mapping not working

Expected behavior

Connecting from inside the container to host

Actual behavior

Connection refused

Information

Starting up a container with:

docker run -d -p 2222:22 -p 127.0.0.1:27017:27017 nitai/r2

Inspect shows that ports are open:

"PortBindings": {
                "22/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "2222"
                    }
                ],
                "27017/tcp": [
                    {
                        "HostIp": "127.0.0.1",
                        "HostPort": "27017"
                    }
                ]
            },
Docker for Mac: version: mac-v1.11.2-beta15
OS X: version 10.11.5 (build: 15F34)
logs: /tmp/20160613-105327.tar.gz
failure: No error was detected
[OK]     docker-cli
[OK]     app
[OK]     menubar
[OK]     virtualization
[OK]     system
[OK]     osxfs
[OK]     db
[OK]     slirp
[OK]     moby-console
[OK]     logs
[OK]     vmnetd
[OK]     env
[OK]     moby
[OK]     driver.amd64-linux

Is this currently not supported?

I am having the same issue. Also using beta 15.
Edit: I found my issue was caused by the application inside docker binding to 127.0.0.1. I changed it to bind to 0.0.0.0 and everything started working. This was not specific to the docker for mac beta.

2 Likes

Oh 0.0.0.0 ? I thought it needs to bind to 127.0.0.1 as outlined in my settings above. I’ll try right now too.

No. Its still mapped to 127.0.0.1 on the host. It was the app I am running inside the container that was binding to localhost.

Yeah, somehow it doesn’t work for me. I’m running a node.js app inside the container and I want it to connect to the MongoDB running on the host. Whatever, I try it fails to connect.

Tried:

Change connection from node.js to mongo to be 0.0.0.0 - fails
Change connection from node.js to mongo to be 127.0.0.1 - fails

Run the container:

docker run -d -p 2222:22 -p 127.0.0.1:27017:27017 nitai/r2
or
docker run -d -p 2222:22 -p 27017:27017 nitai/r2

Can you spot what I’m missing here?

Thank you.

I see. You are connecting from the container to the host (I misread your initial post). In order to do that, you would need the ip of the host from within the container. There isn’t really a good way of doing this yet. You can see the discussion on here:

and now here:

Yes, thanks. However, I’m creating a “customer facing” script where I’m trying to shield of all those things. I think I just have to resort to linking and only that is really supported, i.e. running MongoDB in another container and not on the localhost.

If you have DNS in place, you can simply use your host DNS name (perhaps passing it to the container as an env variable).

  • Edit: make sure that your container is using a DNS that can resolve your host’s hostname!
1 Like

Yes, that would be an alternative. I think for now I simply use links.

Thanks, this was super helpful, setting the Docker configuration in MAC,
Docker->Preferences->Daemon->Advanced
Setting the ip address will be the IP address on which the docker port will map to.

{
  "debug" : true,
  "ip" : "127.0.0.1",
}

Then as per the advise I set my application to run on host - 0.0.0.0