Node Docker Runs, but can't see the application

青春壹個敷衍的年華 提交于 2021-02-04 19:28:06

问题


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.

I've tried tons of variations on this as well.

This is what I see when I run docker inspect <container id>:

Server running at: http://localhost:8080

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

Here's my 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"]

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

回答1:


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

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
});


来源:https://stackoverflow.com/questions/48034906/node-docker-runs-but-cant-see-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!