No access to network from inside a container started with docker-compose

Viewed 314

I have NGINX running inside a container but I want it to proxy upstream hosts on my network.

When I run my container with network_mode: host I expect the container to "see" hosts just as I can on my host/laptop. However CURL is unable to connect:

# curl http://127.0.0.1:8888/
curl: (7) Failed to connect to 127.0.0.1 port 8888: Connection refused

docker-compose.yaml

version: "3.9"
services:
  mynginx:
    build:
      context: ./
      dockerfile: ./Dockerfile
    # Need to be able to access other hosts on network to proxy
    network_mode: host

$ docker network inspect host

[
    {
        "Name": "host",
        "Id": "5ed504e0e0ccb68011d7ce36e08489638e96f6dda88ab9e277aec81220528341",
        "Created": "2021-06-28T07:18:36.1419647Z",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "eac17355dd3004addb521d0ec9cca49cc28be99373260a8754cde9d92650ca21": {
                "Name": "gkpoc_gatekeeper_1",
                "EndpointID": "b4f0bf4c04cbf157c03d9f160caa8ffb31b79ef29639158f3cd5f17d9f27c943",
                "MacAddress": "",
                "IPv4Address": "",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
1 Answers

The mistake was to assume 127.0.0.1 (or localhost) applies to my host/laptop whereas it applies to the container "VM".

It works if you use the actual host IP or an alias provided by Docker host.docker.internal:

curl http://host.docker.internal:8888/
Related