Docker Beta on Mac : Cannot use ip to access nginx

2020-07-18 03:46发布

问题:

I installed the docker-beata (https://beta.docker.com/) for osx. Next, I created a folder with this file docker-compose.yml :

web:
  image: nginx:latest
  ports:
    - "8080:80"

After, I used this command : docker-compose up. Container start with success.

But the problem is to access in my container. I don't know what ip use. I try to find ip with docker ps and docker inspect ...:

"Networks": {
  "bridge": {
    "IPAMConfig": null,
    "Links": null,
    "Aliases": null,
    "NetworkID": "6342cefc977f260f0ac65cab01c223985c6a3e5d68184e98f0c2ba546cc602f9",
    "EndpointID": "8bc7334eff91d159f595b7a7966a2b0659b0fe512c36ee9271b9d5a1ad39c251",
    "Gateway": "172.17.0.1",
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:11:00:02"
  }
}

So I try to use http://172.17.0.2:8080/ to access, but I have a ERR_CONNECTION_TIMED_OUT error. But, if I usehttp://localhost:8080/, I can access to my container ! (But my localhost is already use by my native config on my mac, so if I want use localhost I must stop my native apache).

Why it's doesn't work with the ip ?

回答1:

As @Javier-Segura mentioned, on with native Docker on Linux you should be able to hit the container via it's IP and port, so in your case http://172.17.0.2:80 - the 8080 port would be on the host IP.

With Docker for Mac Beta it does not appear to work the same way for the container. It changes a bit with every release but right now it appears you can not reach a container by ip via conventional means.

Unfortunately, due to limtations in OSX, we’re unable to route traffic to containers, and from containers back to the host.

Your best bet is to use a different non-conflicting port as mentioned. You can use different Compose config files for different environments, so as in the example above, use 8081 for development and 8080 for production, if that is the desire. You would start Compose in production via something like docker-compose -f docker-compose.yml -f production.yml up -d where production.yml has the overrides for that environment.



回答2:

When you map a port (like done with "8080:80") you are basically saying that "Forward the port 8080 on my localhost to the 80 port on the container".

Then you can access your nginx via:

  • http://localhost:8080
  • http://172.17.0.2:80/ (depending on the network configuration)

If the port 8080 is already used by apache on your mac, you can change your configuration to "8081:80" and nginx will be available on 8081



回答3:

Here is one more tip to add to the good ones already provided. You can use the -p option to include IP mapping in addition to your port mapping. If you include no IP (something like -p 8080:80), then your telling docker to route traffic entering all interfaces on port 8080 to your docker internal network (172.17.0.2 in your case). This includes, but is not limited to, localhost. If you'd like this mapping to apply to only a certain IP, for example an IP dynamically assigned to your workstation through DHCP, you can specify the IP in the option as -p 10.11.12.13:8080:80 (where 10.11.12.13 is a fictional IP). Then localhost or any other interface would not be routed.

Likewise, you could use the option to restrict to localhost with -p 127.0.0.1:8080:80 so that other interface traffic is not routed to your docker container's 172.17.0.2 interface.



回答4:

@pglezen is right. Providing full IP within compose file is solving the issue. Image IP addresses that were generated by docker-compose dose not work (now) on MAC OSX.

Providing specific ip within compose file allowed to access container image:

nginx: image: nginx:latest ports: - "127.0.0.1:80:80" links: - php-fpm

docker-compose still assigned generic 172.* IP address to image that was not accessable. But real hardcoded 127.0.0.1 was working and returns correct container response.