容器正在运行但无法在浏览器中访问

时间:2017-04-18 14:38:35

标签: docker docker-compose

我跟着http://containertutorials.com/docker-compose/flask-compose.html并且有一个正在运行的容器。

$ docker-compose up
Starting flaskcomposedockerexample_web_1
Attaching to flaskcomposedockerexample_web_1
web_1  |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1  |  * Restarting with stat
web_1  |  * Debugger is active!

$ docker logs d66ca374c99a
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 249-821-232

转到浏览器或卷曲将无法连接:

This site can’t be reached

127.0.0.1 refused to connect.

$ curl 127.0.0.1:5000
curl: (7) Failed to connect to 127.0.0.1 port 5000: Connection refused

为什么无法访问没有错误的容器?谢谢

$ netstat 
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  142.25.150.89.57766    stackoverflow.co.https ESTABLISHED

tcp4       0      0  142.25.150.89.56837    8.36.126.32.http       ESTABLISHED
tcp4       0      0  localhost.56800        localhost.56800        ESTABLISHED
tcp4       0      0  localhost.56801        localhost.56801       ESTABLISHED


$ docker inspect d66ca374c99a
[
    {
        "Id": "d66ca374c99afb13a2bee56fa44fd73f4c39569edcf8c50f175bf5a124a57a73",
        "Created": "2017-04-18T14:08:54.859932607Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 14402,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-04-18T14:09:28.342011012Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:1b3ceada8dd91e8a4403e7ee001b7a4832f72120162f8788651c3301f5ccfa89",
        "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/d66ca374c99afb13a2bee56fa44fd73f4c39569edcf8c50f175bf5a124a57a73/resolv.conf",
        "HostnamePath": "/mnt/sda1/var/lib/docker/containers/d66ca374c99afb13a2bee56fa44fd73f4c39569edcf8c50f175bf5a124a57a73/hostname",
        "HostsPath": "/mnt/sda1/var/lib/docker/containers/d66ca374c99afb13a2bee56fa44fd73f4c39569edcf8c50f175bf5a124a57a73/hosts",
        "LogPath": "/mnt/sda1/var/lib/docker/containers/d66ca374c99afb13a2bee56fa44fd73f4c39569edcf8c50f175bf5a124a57a73/d66ca374c99afb13a2bee56fa44fd73f4c39569edcf8c50f175bf5a124a57a73-json.log",
        "Name": "/flaskcomposedockerexample_web_1",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users/cchilders/flask_compose_docker_example:/code:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "5000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "5000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": null,
            "Name": "aufs"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/cchilders/flask_compose_docker_example",
                "Destination": "/code",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "d66ca374c99a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "app.py"
            ],
            "ArgsEscaped": true,
            "Image": "flaskcomposedockerexample_web",
            "Volumes": {
                "/code": {}
            },
            "WorkingDir": "/app",
            "Entrypoint": [
                "python"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "3c65c28cc5926f591068f82a4d028ae50573b14ac4059ebe17b196be2185995a",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "flaskcomposedockerexample",
                "com.docker.compose.service": "web",
                "com.docker.compose.version": "1.11.2"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "6b4cd91f76cb21f70fc7b1b17e403f21ef790cc1e02fee5547740ac5192229fe",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/6b4cd91f76cb",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "e05d8bf26815734a146d6f4b1fb803b365629dfce732a2b21993970a33e54a80",
            "Gateway": "162.16.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "162.16.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:bd:55:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2fd3c33ec08105cd00de10ab5555554572bac4548866239aa375e8df60ed3aa",
                    "EndpointID": "e05d8bf26815734a146d6f4b1fb80555559dfce732a2b21993970a33e54a80",
                    "Gateway": "162.16.0.1",
                    "IPAddress": "162.16.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:44:00:04"
                }
            }
        }
    }
]



$ netstat -tnlp
netstat: option requires an argument -- p
Usage:  netstat [-AaLlnW] [-f address_family | -p protocol]
    netstat [-gilns] [-f address_family]
    netstat -i | -I interface [-w wait] [-abdgRtS]
    netstat -s [-s] [-f address_family | -p protocol] [-w wait]
    netstat -i | -I interface -s [-f address_family | -p protocol]
    netstat -m [-m]
    netstat -r [-Aaln] [-f address_family]
    netstat -rs [-s]

$ netstat -tnpl
netstat: l: unknown or uninstrumented protocol

1 个答案:

答案 0 :(得分:3)

首先你要引用的教程在app.py的第一行中有语法错误有两个问题应该是

from flask import Flask 

其次,docker在Mac上的工作方式不同,windows docker并没有直接将端口映射到本地Host而不是docker创建一个大小约为24 mb的非常小的Linux虚拟机,然后在其中运行容器这个虚拟机也称为docker机器,所以不是将端口映射到localhost docker而是将它映射到docker机器,这样你的容器就可以访问了

http://docker_machine_ip:port_no 

而不是

http://localhost:port_no

可以使用

找到默认的docker machine ip
 docker-machine ip dev