Docker:来自服务器的空响应

Hal*_*mak 1 node.js docker

我在连接docker容器时遇到问题,服务器返回空响应,但配置似乎正确。

当我使用docker-compose up命令时,一切似乎都可以正常工作,但是我收到来自服务器的空响应。

我仔细检查了端口映射,但是我什么都没注意到。

这是撰写文件

    version: '3'

    services: 
      authapi:
        build:
          dockerfile: ./docker/Dockerfile.dev
          context: .
        restart: always
        volumes:
          - .:/usr/src/auth
          - /usr/src/auth/node_modules
        environment: 
          FBAPPID: ${FBAPPID}
          FBAPPSECRET: ${FBAPPSECRET}
          GOOGLEAPPID: ${GOOGLEAPPID}
          GOOGLEAPPSECRET: ${GOOGLEAPPSECRET}
          GITHUBAPPID: ${GITHUBAPPID}
          GITHUBAPPSECRET: ${GITHUBAPPSECRET}
        ports:
          - ${PORT}:${PORT}
        command: 
          - sh
          - -c
          - sleep 20 && npm run start:dev
        depends_on: 
          - psql
      psql:
        image: postgres:10-alpine
        ports:
          - 5432:5432
        environment: 
          POSTGRES_USER: ${PG_USER}
          POSTGRES_PASSWORD: ${PG_PASSWORD}
          POSTGRES_DB: ${PG_DB}
        volumes:
          - ./pg-data:/var/lib/postgresql/data
Run Code Online (Sandbox Code Playgroud)

Docker文件

    FROM node:10-alpine

    WORKDIR /usr/src/auth

    COPY ./package.json .

    RUN npm install

    COPY . . 

    EXPOSE 9091

    CMD ["npm", "run", "start:prod"]
Run Code Online (Sandbox Code Playgroud)

码头工人端口响应

9091/tcp -> 0.0.0.0:9091
Run Code Online (Sandbox Code Playgroud)

docker ps响应

CONTAINER ID        IMAGE                 COMMAND                  
CREATED             STATUS              PORTS                    NAMES
cecbf815523e        authservice_authapi   "sh -c 'sleep 20 && …"   15 
minutes ago      Up 15 minutes       0.0.0.0:9091->9091/tcp   
authservice_authapi_1
4e5c5f312703        postgres:10-alpine    "docker-entrypoint.s…"   15 
minutes ago      Up 15 minutes       0.0.0.0:5432->5432/tcp   
authservice_psql_1
Run Code Online (Sandbox Code Playgroud)

这是码头工人网络

NETWORK ID          NAME                  DRIVER              SCOPE
3bb18805e2b1        authservice_default   bridge              local
f7fb02e95fc3        bridge                bridge              local
815dccd6c8b8        host                  host                local
12ca56c3c08d        none                  null                local
Run Code Online (Sandbox Code Playgroud)

authservice_default检查响应

        [
      {
          "Name": "authservice_default",
          "Id": "3bb18805e2b129342ce255017c88b8d72717f050419503d81042f5319b5f5009",
          "Created": "2018-09-26T18:24:01.3773204Z",
          "Scope": "local",
          "Driver": "bridge",
          "EnableIPv6": false,
          "IPAM": {
              "Driver": "default",
              "Options": null,
              "Config": [
                  {
                      "Subnet": "172.18.0.0/16",
                      "Gateway": "172.18.0.1"
                  }
              ]
          },
          "Internal": false,
          "Attachable": true,
          "Ingress": false,
          "ConfigFrom": {
              "Network": ""
          },
          "ConfigOnly": false,
          "Containers": {
              "4e5c5f3127036fe300ec2917fd6023d00ca8d3c4e28b583107c2deabc6fd2dda": {
                  "Name": "authservice_psql_1",
                  "EndpointID": "461b8164f174d436a4a039ff9ff494cac3bc5f6f96c1b8d0619928c8dafb6652",
                  "MacAddress": "02:42:ac:12:00:02",
                  "IPv4Address": "172.18.0.2/16",
                  "IPv6Address": ""
              },
              "cecbf815523e4028c2b9cb1d74104ef61cc8fb8947624c9df8f035193842a9b0": {
                  "Name": "authservice_authapi_1",
                  "EndpointID": "af30a79b70d675002f12bd61ca8aed2d8d5f036bd99c10fa7a205cf3e21ae19b",
                  "MacAddress": "02:42:ac:12:00:03",
                  "IPv4Address": "172.18.0.3/16",
                  "IPv6Address": ""
              }
          },
          "Options": {},
          "Labels": {
              "com.docker.compose.network": "default",
              "com.docker.compose.project": "authservice",
              "com.docker.compose.version": "1.22.0"
          }
      }
    ]
Run Code Online (Sandbox Code Playgroud)

集装箱原木

    [nodemon] starting `node ./src/babel.start.js`
    {"level":30,"time":1537987440400,"msg":"Server listening at http://127.0.0.1:9091","pid":63,"hostname":"cecbf815523e","v":1}
    |-----------------------------Start-----------------------------|
    |                                                               |
    |              Server started on http://127.0.0.1:9091
    |                                                               |
    |---------------------------------------------------------------|
    |-----------------------------CPU INFO--------------------------|
    |                                                               |
    |  Cpu 0: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz => speed 2697
    |  Cpu 1: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz => speed 2697
    |                                                               |
    |----------------------TOTAL MEMORY ----------------------------|
    |                                                               |
    |========================> 2 GB
    |                                                               |
    |-----------------------FREE MEMORY-----------------------------|
    |                                                               |
    |========================> 155.5 MB
    |                                                               |
    |------------------------PLATFORM-------------------------------|
    |                                                               |
    |========================> linux v4.9.93-linuxkit-aufs x64
    |                                                               |
    |---------------------------------------------------------------|
    |------------------------IFACE IP-------------------------------|
    |                                                               |
    |========================> 172.18.0.3
    |                                                               |
    |---------------------------------------------------------------|
Run Code Online (Sandbox Code Playgroud)

i dont add here postgress one because application has nothing to do with postgress now.

Everything seems fine from my perspective, am i missing something ? Huge thanks already now for those who tries to help :)

Hal*_*mak 5

我已经通过将应用程序绑定到0.0.0.0来解决了这个问题,一切都开始工作了。

我希望对此类问题寻求解决方案的任何人都会有用。

  • 我在使用nodejs fastify时遇到了同样的问题。必须绑定`0.0.0.0`。`Fastify.listen(3000, '0.0.0.0', err => {` (7认同)
  • 如何绑定到“0.0.0.0”将取决于您运行的应用程序服务器的类型。对我来说,我正在运行 python 的“gunicorn”,它[默认绑定到 127.0.0.1:8000](https://docs.gunicorn.org/en/stable/settings.html#bind)。我只需在运行“gunicorn”命令时添加一个标志“-b 0.0.0.0:8000”即可将其绑定到该地址。 (5认同)
  • 你是如何将它准确绑定到 0.0.0.0 的? (4认同)
  • 我使用的是 Nodejs 和 Express,所以我将 Express 应用程序绑定到 0.0.0.0 (2认同)
  • 对于 NodeJS Express 我用 `express().listen('8090', '0.0.0.0');` 启动应用程序 (2认同)