docker-compose服务无法通过其名称访问其他服务

时间:2020-08-17 21:09:54

标签: docker docker-compose dockerfile

我对Docker还是很陌生,但是在看了一些教程之后,我发现如果我希望我的服务具有通用代码,就必须使用docker-compose。

这是我的目录结构:

- project
   - docker-compose.yml
   - packages
       - common
          - src
             - eureka
                - eureka-helper.js
       - gateway
          - Dockerfile
          - src 
             - (some more directories and files)
          - server.js
       - users
          - Dockerfile
          - src 
             - app.js
             - (some more directories and files)
version: '3'

services:
    eureka:
        image: springcloud/eureka
        ports:
            - "8761:8761"
    gateway: 
        build:
            context: ./
            dockerfile: ./packages/gateway/Dockerfile
        ports:
            - "3000:3000"
        links:
            - eureka
    users: 
        build:
            context: ./
            dockerfile: ./users/Dockerfile
        ports:
            - "3004:3004"
        links:
            - mongo
            - eureka
    mongo:
        image: mongo
        volumes:
            -  C:\Users\myUser\docker\mongodb-data:/data/db
        ports: 
            - "27017:27017"

我的server.js(内部网关)和app.js(内部用户)在eureka-helper中调用了一个向Eureka注册的函数。

eurekaHelper.registerWithEureka('gateway', eureka, port);

但是似乎server.js没有此尤里卡的值:

gateway_1  | /opt/app/gateway/server.js:27
gateway_1  | eurekaHelper.registerWithEureka('gateway', eureka, port);
gateway_1  |                                            ^
gateway_1  |
gateway_1  | ReferenceError: eureka is not defined

按照这样的说法:https://docs.docker.com/compose/networking/,看来我真的不需要docker-compose中的链接:

链接允许您定义其他服务可以通过其访问服务的额外别名。他们不需要启用服务进行通信-默认情况下,任何服务都可以使用该服务的名称访问其他任何服务。

...但是网关仍然无法到达尤里卡。

知道我是否缺少任何东西。

当我在网关中检查时:

        "Networks": {
            "*project*_default": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "gateway",
                    "a358b371de9d"
                ],
                "NetworkID": "d986ee77bf6181e9dd59711650359dc49fd8ce3fc34c6772133a0049ab55bd3b",
                "EndpointID": "9633a7affe435793345342d4f6d00d5bef43d2267676448dfde0f97d75d5f7af",
                "Gateway": "172.19.0.1",
                "IPAddress": "172.19.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:13:00:03",
                "DriverOpts": null
            }
        }

当我在尤里卡(Eureka)进行检查时:

        "Networks": {
            "*project*_default": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "eureka",
                    "17a218bf6e9e"
                ],
                "NetworkID": "d986ee77bf6181e9dd59711650359dc49fd8ce3fc34c6772133a0049ab55bd3b",
                "EndpointID": "43f4a9eb9efd4d30ad65d2c943065b8fb32ed50822bd553e07a2b0020c173fec",
                "Gateway": "172.19.0.1",
                "IPAddress": "172.19.0.2",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:13:00:02",
                "DriverOpts": null
            }
        }

0 个答案:

没有答案