32

Previously I was using the Docker toolkit and could use docker-machine inspect <machine-name> to find the IP of the docker machine, in order to view the pages being served by the container within.

I have upgraded to the docker beta client (for mac) and cannot find where the pages are being served.

I assume because it's docker pure and not docker-machine I would see the exposed ports on localhost. But they are not open. Even via kitematic I navigate through to the exposed port (80) but see nothing.

$ docker inspect tiny_cori | jq '.[].HostConfig.PortBindings'
{
  "80/tcp": [
    {
      "HostIp": "",
      "HostPort": "80"
    }
  ]
}

$ docker inspect tiny_cori | jq '.[].NetworkSettings.IPAddress'
"172.17.0.2"

$ curl 172.17.0.2:80/status
^C

$ docker exec -t -i tiny_cori /bin/bash

root@f1c303f60a15:/# curl localhost:80/status
{"status":"OK"}

root@f1c303f60a15:/# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1
    link/sit 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1
    link/tunnel6 :: brd ::
4: ip6gre0@NONE: <NOARP> mtu 1448 qdisc noop state DOWN group default qlen 1
    link/gre6 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 brd 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
26: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever

Is 172.17.0.2 really the IP of my container? If not, where can I find what the IP is? Or do I have a port mapping problem?

6 Answers 6

35

You're looking at the IP address inside the container which is 172.17.0.2 and only exists "inside" the docker containers (and between them).

The IP address you want to connect to is your host machines IP address. Do an ifconfig on your Mac and find the right one (typically 192.168.x.y or 10.x.y.z). Localhost should also work from outside the container.

8
  • 8
    Not sure who downvoted this but it's actually the correct answer. With Docker for Mac, there is no longer an intermediary VM with a separate machine IP address. Hitting the host (either via IP or localhost) should forward the requests to the container as long as the ports are mapped correctly.
    – DNJohnson
    Jul 22, 2016 at 12:33
  • Thanks. I was confused why this had been downvoted (twice). Maybe because it just seems too simple to be the actual answer. Jul 24, 2016 at 3:39
  • I didn't downvote this. But I suspect there's a subset of users who, for whatever reason, this straightforward answer doesn't (or didn't) work. I'm marking as correct.
    – Synesso
    Aug 2, 2016 at 23:55
  • 2
    Well heres an interesting thing - on OS X when I go to my xHyve VM via screen command and run curl localhost:8080 - I get response from one of the containers. But if I do the same in OS X terminal - connection refused. How can this happen?
    – mvmn
    Dec 13, 2016 at 11:10
  • 1
    P.S. This happened to a container created via docker-compose.
    – mvmn
    Dec 13, 2016 at 11:11
14

type following command and you will get your docker container ip

$docker inspect <docker-container-name>

Scroll down and you will get IP-address under "NetworkSettings"

2
  • Thank you. I neglected to mention that I tried this & it didn't work. My question has been updated.
    – Synesso
    May 24, 2016 at 7:51
  • Right, but you can't ping it. I suspect that this is why the above correct answer gets downvoted. The question I am left with is whether there is a way to bridge to the network on the virtual host. The virtual host is still there if better integrated into MacOS, and I'm sure you can ping these IP addresses from there, but can't I bridge to that network so they are locally available on the Mac? Nov 11, 2016 at 11:03
8

Docker for Mac runs on IP address 127.0.0.1

2
  • 1
    that is not entirely a false statement :P
    – jonas
    Sep 23, 2016 at 19:20
  • In fact, this was the most straightforward answer that solved my problem.
    – James
    Apr 18, 2017 at 0:08
1

The ip address is explicitly mentioned in the output to docker ps.

$ docker ps
CONTAINER ID        ... PORTS
c785a183b376        ... 0.0.0.0:80->80/tcp, 443/tcp

$ curl 0.0.0.0:80/status
{"STATUS":"OK"}

Furthermore, localhost is not the same as 0.0.0.0

3
  • 5
    0.0.0.0 means all ip addresses on the current machine and is usually used when listening, not connecting. I don't understand how the above curl would even work. May 24, 2016 at 8:46
  • 2
    The 0.0.0.0 is the ip address the service is bound to in the container, and means nothing in the context of the host running it. the output under the PORTS column is saying that the container's port 80 (0.0.0.0:80) is mapped to port 80 locally (80/tcp), as well as port 443 locally.
    – Kevin
    Aug 2, 2016 at 20:14
  • 1
    No, it isn't the same, but as pointed out, 0.0.0.0 isn't an IP address, it is a signal to listen that I want all of them including localhost. In the context of connecting they are the same unless the service is bound to a specific IP (localhost or the external IP). Nov 11, 2016 at 11:13
0

I believe that 127.0.0.1 is for localhost.

To get the docker id that running on your Mac. Run "ifconfig" in the terminal and will have it in the result besides the 127.0.0.1

Mostly something like 192.168.x.y

Hope it helps.

0

From 18.03 onwards the recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.

see https://docs.docker.com/docker-for-mac/networking/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .