本文介绍了Node Docker运行,但看不到该应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来我的Hapi应用程序正在Docker容器中运行,但无法在浏览器中找到它.我以为 docker run -d -p 8080:3000 可以做到,但我想没有.我正在引导至docker,并且 http://localhost:8080/hello http://192.168.99.100:8080/hello 均不起作用.

It appears that my Hapi app is running in a Docker container, but I can't hit it in the browser. I thought that docker run -d -p 8080:3000 would have done it, but I guess not. I'm running boot to docker and neither http://localhost:8080/hello nor http://192.168.99.100:8080/hello is working.

我也尝试过很多变化.

这是我运行 docker inspect&:

Server running at: http://localhost:8080

这是我的Hapi.js服务器:

Here's my Hapi.js server:

'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:

Here's my Dockerfile:

FROM node:8.9.3

MAINTAINER My Name <[email protected]>

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:

Here's my 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,而在于如何配置节点服务器.

The issue is not with Docker but how you configure the node server.

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

If you bind to localhost it will only be available from within the docker container. If you want to allow connections from the docker host either don't provide a hostname or use 0.0.0.0.

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

这篇关于Node Docker运行,但看不到该应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:22