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



看起来我的快乐应用程序正在 Docker 容器中运行,但我无法在浏览器中点击它。我以为docker run -d -p 8080:3000会这样做,但我想不会。我正在运行 bootto docker,http://localhost:8080/hellohttp://192.168.99.100:8080/hello 都不起作用。

我也尝试了很多变化。

这是我运行docker inspect <container id>时看到的:

Server running at: http://localhost:8080

这是我快乐.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"]

这是我的包.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
});

相关内容

最新更新