简体   繁体   English

Docker:无法从主机连接到容器

[英]Docker: Unable to connect to container from host

I try to use docker-compose in order to run an app which consists of 2 container: akka app and postgresql. 我尝试使用docker-compose来运行包含2个容器的应用程序:akka应用程序和postgresql。 Here is yaml file: 这是yaml文件:

version: "3"
services:
  api:
    image: akka-app:latest
    ports:
        - "9000:5000"
  db:
    image: postgres-db:latest

After the docker-compose up command I see that containers are up and running using docker ps command: 在执行docker-compose up命令后,我看到使用docker ps命令启动并运行了容器:

CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                    NAMES
65326e5a9677        akka-app:latest                      "java -cp /..."   40 minutes ago      Up 23 seconds       0.0.0.0:9000->5000/tcp   samplecompose_api_1
1f86273c4f9a        postgres-db:latest                   "docker-ent..."   19 hours ago        Up 23 seconds       5432/tcp                 samplecompose_db_1

But when I try to access the akka-app from the host via the GET request to 0.0.0.0:9000/status or localhost:9000/status I get back "refused to connect" error in the postman. 但是,当我尝试通过GET请求从主机访问akka-app0.0.0.0:9000/statuslocalhost:9000/status我在邮递员中收到“拒绝连接”错误。

Extra information: 额外的信息:

  • Docker version 17.06.0-ce, build 02c1d87 Docker版本17.06.0-ce,内部版本02c1d87
  • docker-compose version docker-compose版本
  • 1.14.0, build c7bdf9e MacOS Sierra 10.12.5 1.14.0,构建c7bdf9e MacOS Sierra 10.12.5

Network settings of the akka-app container: akka-app容器的网络设置:

"NetworkSettings": {
    "Bridge": "",
    "SandboxID": "5b5b322ffb0fb881317f8cb4e69b8fd887566b8c5b73940174aabace35d816f8",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {
        "5000/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "9000"
            }
        ]
    },
    "SandboxKey": "/var/run/docker/netns/5b5b322ffb0f",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "",
    "Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "",
    "IPPrefixLen": 0,
    "IPv6Gateway": "",
    "MacAddress": "",
    "Networks": {
        "pinpointcompose_default": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": [
                "api",
                "65326e5a9677"
            ],
            "NetworkID": "925c8379c6d5b334177d7d198d1d38e29983903474183bee5734df9d30ab9810",
            "EndpointID": "c7c37f545b00c6a19863315858e93a79f10b54b5675dc2cb8704d2f2f9b2bb1c",
            "Gateway": "172.18.0.1",
            "IPAddress": "172.18.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:12:00:02"
        }
    }
}

UPDATE: 更新:

docker-compose output: docker-compose输出:

docker-compose up
Starting samplecompose_api_1 ... 
Starting samplecompose_api_1
Starting samplecompose_db_1 ... 
Starting samplecompose_api_1 ... done
Attaching to samplecompose_db_1, samplecompose_api_1
db_1   | LOG:  database system was shut down at 2017-07-06 08:54:56 UTC
db_1   | LOG:  MultiXact member wraparound protections are now enabled
api_1  | 09:04:26.030 [default-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
api_1  | 09:04:27.472 [default-akka.actor.default-dispatcher-4] INFO sample-service - Bound to /0.0.0.0:5000

The problem was in an old VirtualBox installed who knows when on my laptop. 问题出在安装了旧VirtualBox的谁知道我的笔记本电脑上的时间。 So while it was installed I could not access running docker containers via localhost ... You can read more about this on official docker site 因此,在安装后,我无法通过localhost访问正在运行的Docker容器...您可以在官方Docker站点上了解更多有关此的信息

After I uninstalled the VirtualBox from the laptop, then restarted the Docker app and relaunched the terminal sessions it was unreal to use localhost for access to the containers. 从笔记本电脑卸载VirtualBox,然后重新启动Docker应用并重新启动终端会话后,使用localhost访问容器是不现实的。

Instead of the localhost I used the IP which I got from the docker-machine ip default . 我使用从docker-machine ip default那里获得的IP代替了localhost。 But this is not the good way. 但这不是好方法。

So ensure once again that you meet all of the requirements. 因此,请再次确保您满足所有要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM