【问题标题】:Node Docker Runs, but can't see the application节点 Docker 运行,但看不到应用程序
【发布时间】:2020-10-11 09:46:13
【问题描述】:

我的 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();

这是我的 Dockerfile:

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"
    }
}

【问题讨论】:

    标签: node.js docker hapijs


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      相关资源
      最近更新 更多