Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access "http://localhost" from a Docker container in Windows?

I have a set of containers running in Windows 10 with Docker, this is the output of docker-compose ps:

> docker-compose ps
     Name                    Command               State                          Ports
--------------------------------------------------------------------------------------------------------------
db_mysql          docker-entrypoint.sh mysqld      Up      3306/tcp
elk               /usr/bin/supervisord -n -c ...   Up      0.0.0.0:81->80/tcp
php71-fpm-nginx   /config/bootstrap.sh             Up      443/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:9001->9001/tcp

And this is the output of docker inspect:

> docker inspect php71-fpm-nginx
[
    {
        ...
            "NetworkMode": "anotherlampdocker_default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ],
                "9001/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "9001"
                    }
                ]
            },
        ...
        "Mounts": [
            {
                "Source": "/d/Development/www",
                "Destination": "/data/www",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            ...
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "b96cf4c0f2c17d65659c31982b9200a79cca6f1c214770d31938204c493a6720",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "443/tcp": null,
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ],
                "9001/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "9001"
                    }
                ]
            },
            ...
            "Networks": {
                "anotherlampdocker_default": {
                    "IPAMConfig": null,
                    "Links": [
                        "db_mysql:db",
                        "db_mysql:db_mysql"
                    ],
                    "Aliases": [
                        "php-fpm",
                        "248e8c254eee"
                    ],
                    "NetworkID": "de1e10b63e6e5050809af59ac4d26b7cb691afd5805d1cf7f0492c702814f34d",
                    "EndpointID": "ccec71967c6100c5a9f3ad82d82bbb2a371f77e12c493bf05bfd15f2d188ce00",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:03"
                }
            }
        }
    }
]

I am trying to access http://localhost but I can't. I've tried also using the container IP meaning http://172.18.0.3 and doesn't work either.

When I say I can't it means the following message:

This site can’t be reached 172.18.0.3 took too long to respond.

The same container works perfectly in Linux.

This is the content of c:\Windows\System32\drivers\etc\hosts file:

# localhost name resolution is handled within DNS itself.
    127.0.0.1       localhost

The ports from the container are mapped to the host in the docker-compose.yml file:

services:
  php-fpm:
    container_name: "php71-fpm-nginx"
    build: php-fpm
    ports:
        - 80:80
        - 9001:9001
    environment:
        PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
        STATUS_PAGE_ALLOWED_IP: '127.0.0.1'
    volumes:
        - D:\Development\www\:/data/www

What I am missing here?

like image 586
ReynierPM Avatar asked Dec 15 '16 01:12

ReynierPM


People also ask

Can I access localhost from Docker?

Accessing the Host With the Default Bridge ModeYou just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

How do I connect to a docker container locally?

To connect to a container using plain docker commands, you can use docker exec and docker attach . docker exec is a lot more popular because you can run a new command that allows you to spawn a new shell. You can check processes, files and operate like in your local environment.

How do I make my Docker talk to localhost?

A simple solution to this in a Linux machine is to use the --network=”host” option along with the Docker run command. After that, the localhost (127.0. 0.1) in your Docker container will point to the host Linux machine. This runs a Docker container with the settings of the network set to host.


1 Answers

You can get the docker machine IP and access the application:
1. Using command docker-machine : docker-machine ip
2.or By login to the docker image which is created when you start the docker and getting the eth1 ip

Then try : [docker-machine ip]:[port]

like image 63
Savio Mathew Avatar answered Sep 17 '22 17:09

Savio Mathew