'Unable to access the web server when dockerising the Hapi JS

I am building a web application Node JS web framework, Hapi JS, https://hapi.dev/. I am trying to dockerise my application. But it is not working.

I first set up npm for the project running the following command.

npm init

Then I installed hapi js running the following command.

@hapi/hapi

Then I created the server.js file with the following code.

'use strict';

const Hapi = require('@hapi/hapi');

const init = async () => {

    const server = Hapi.server({
        port: 4000,
        host: 'localhost'
    });

    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

When I run node server.js in the terminal and go to the localhost:4000 in the browser, I can see the Hello World! message.

Then I started dockerising the application. For that I created the Dockerfile with the following content.

FROM node:14-alpine
RUN apk update && apk upgrade
RUN apk add nodejs
RUN rm -rf /var/cache/apk/*
COPY . /
RUN cd /; npm install
EXPOSE 4000
CMD ["node", "/server.js"]

Then I created docker-compose.yaml file with the following content.

version: '3.8'

services:
  server:
    container_name: hapi-web-server
    build: .
    ports:
      - 4000:4000

Then I run docker-compose up -d in the terminal and when I go to the localhost:4000 in the browser, I am seeing the following error.

enter image description here

What is wrong with my code and how can I fix it?



Solution 1:[1]

change host value host: '0.0.0.0'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Def Soudani