如何访问"http://localhost"从Windows中的Docker容器中获取? [英] How to access "http://localhost" from a Docker container in Windows?

查看:116
本文介绍了如何访问"http://localhost"从Windows中的Docker容器中获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组在Windows 10中使用Docker运行的容器,这是 docker-compose ps 的输出:

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

这是 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"
                }
            }
        }
    }
]

我正在尝试访问 http://localhost ,但是不能.我也尝试使用容器IP来表示 http://172.18.0.3 ,它也不起作用.

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:

无法访问此网站172.18.0.3花了很长时间才做出回应.

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

同一个容器在Linux中完美运行.

The same container works perfectly in Linux.

这是 c:\ Windows \ System32 \ drivers \ etc \ hosts 文件的内容:

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

容器中的端口在 docker-compose.yml 文件中映射到主机:

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

我在这里想念什么?

推荐答案

您可以获取Docker机器IP并访问应用程序:
1.使用命令 docker-machine : docker-machine ip
2.or登录到启动docker并获取eth1 ip时创建的docker镜像

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

然后尝试:[docker-machine ip]:[端口]

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

这篇关于如何访问"http://localhost"从Windows中的Docker容器中获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆