Search code examples
node.jsdockerhapi.js

Node Docker Runs, but can't see the application


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

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

Solution

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