节点 Docker 运行,但看不到应用程序

Node Docker Runs, but can't see the application

我的 Hapi 应用程序似乎 运行 正在 Docker 容器中,但我无法在浏览器中点击它。我以为 docker run -d -p 8080:3000 会做到这一点,但我想不会。我正在 运行 启动到 docker,但 http://localhost:8080/hellohttp://192.168.99.100:8080/hello 都没有工作。

我也尝试过很多变体。

这是我在 运行 docker inspect <container id>:

时看到的
Server running at: http://localhost:8080

这是我的 Hapi.js 服务器:

'use strict';

const Hapi = require('hapi');

// Create a server with a host and port
const server = Hapi.server({
    host: 'localhost',
    port: 3000
});

// Add the route
server.route({
    method: 'GET',
    path:'/hello',
    handler: function (request, h) {
        return 'hello world';
    }
});

async function start() {

    try {
        await server.start();
    }
    catch (err) {
        console.log(err);
        process.exit(1);
    }

    console.log(`App running at: ${server.info.uri}/hello`);
}

start();

这是我的 Docker 文件:

FROM node:8.9.3

MAINTAINER My Name <email@email.com>

ENV NODE_ENV=production
ENV PORT=3000
ENV user node

WORKDIR /var/www
COPY package.json yarn.lock ./

RUN cd /var/www && yarn

COPY . .

EXPOSE $PORT

ENTRYPOINT ["yarn", "start"]

这是我的 package.json:

{
    "name": "my-app",
    "version": "1.0.0",
    "repository": "https://github.com/myname/myrepo.git",
    "author": "My Name",
    "license": "MIT",
    "private": true,
    "dependencies": {
        "hapi": "17.2.0"
    },
    "scripts": {
        "start": "node ./src/server"
    }
}

问题不在于 Docker,而在于您如何配置节点服务器。

如果绑定到 localhost,它将只能在 docker 容器中使用。如果您想允许来自 docker 主机的连接,请不要提供主机名或使用 0.0.0.0.

const server = Hapi.server({
    host: '0.0.0.0',
    port: 3000
});