1

I've reviewed several posts on this topic and can't yet find a resolution that works for me
https://forums.docker.com/t/how-can-i-navigate-to-container-website-from-host-browser/25035
How to access Docker container's web server from host

Can't connect to static website in Docker container from host.
I have followed this tutorial
https://tecadmin.net/tutorial/docker-run-static-website
the docker container from that effort runs and when I exec into it I can curl the website successfully from the host I cannot connect to the website in the docker container

I can't post screen shots yet so...
-----------------------------------------
My host is windows 10
I am using Oracle VirtualBox
docker version returns

 Version:           19.03.1
 API version:       1.40
 Go version:        go1.12.7
 Git commit:        74b1e89e8a
 Built:             Wed Jul 31 15:18:18 2019
 OS/Arch:           windows/amd64
 Experimental:      false
Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:49:35 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683  

----------------------------------------
Dockerfile
-----------------------------------------

COPY . /usr/share/nginx/html
EXPOSE 127.0.0.1:8080

-----------------------------------------
Docker build command used
-----------------------------------------
docker build -t img-static-site-example .
-----------------------------------------
Docker run command used
-----------------------------------------
docker run -it -d -p 127.0.0.1:8080:80 img-static-site-example
-----------------------------------------
curl to check
------------------------------------------
none of the below work from the host

curl http://172.17.0.3
curl http://172.17.0.3:8080
curl http://localhost:8080

curl http://192.168.99.102
curl http://192.168.99.102:80
curl http://192.168.99.102:8080
curl http://192.168.99.102:2376

----------------------------------------
docker-machine ip returns 192.168.99.102
-----------------------------------------

        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "43d2a1e8e8429e48b5dde91385d5933e2fa095d868b67f1d6ed59a1aca1e1665",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "127.0.0.1",
                        "HostPort": "8080"
                    }
                ],
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/43d2a1e8e842",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "530b098a8d01838fa4e408033a905a3f9f8b8c538a8cefa13a937b24ff325136",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2436a76997d6a454900798af80bf6c35482397acd66a44a07e411d6617e56339",
                    "EndpointID": "530b098a8d01838fa4e408033a905a3f9f8b8c538a8cefa13a937b24ff325136",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
bradmax
  • 11
  • 1
  • 3
  • If you're using Docker Toolbox or Docker Machine, I suspect `docker run -p 127.0.0.1:...` makes the container unreachable. Try removing that fragment of that option (`docker run -p 8080:80`); then you should be able to connect to the `docker-machine ip` address (`http://192.168.99.102:8080`). The `docker inspect` IP address is unreachable and there's no reason to look it up. – David Maze Dec 31 '21 at 17:32
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 09 '22 at 02:30

1 Answers1

0

You seem to be using a setup based on Docker Toolbox or Docker Machine. These run a dedicated Linux VM to run Linux containers (you mention VirtualBox in the question). In this setup, a couple of the Docker networking options don't work the way you might expect on a native-Linux system. You can start a container with --network host, for example, but it will use the VM's host network and not the containing system.

In particular, in this environment, the docker run -p 127.0.0.1:... option starts a container that publishes a port bound to the VM's localhost interface. This is separate from the containing host's localhost interface and any individual container's concept of localhost. This also means that the published port will only be reachable from the Docker VM itself, not from anywhere else; the containing host can't reach it.

In this setup, you need to:

  1. Remove 127.0.0.1 from the docker run -p option; docker run -p 8080:80 ....
  2. Connect to the docker-machine ip address and the first docker run -p port number; http://192.168.99.102:8080.

These constraints are specific to Docker Machine/Docker Toolbox. On a native Linux host or using the Docker Desktop application, you can use localhost as you describe.

The docker inspect IP address is unreachable in almost all of these setups (it only works from the same host on native Linux) and you pretty much never need to look it up.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • That works fine for the case where I'm accessing the static site as http://192.168.99.102:8080 Thanks However, in this particular application I need to be able to reference the site thru a domain alias such as http://myapplication/mydomain.com/login when I host the static site directly on my windows host, I set the /etc/hosts file to map the domain. I'm getting lost in trying to get thru the docker toolbox with that intact – bradmax Jan 03 '22 at 16:14