Encountering difficulty reaching the web server while attempting to dockerize Hapi JS

I have been working on a web application using the Node JS web framework, Hapi JS. Unfortunately, I am facing some issues with dockerizing my application.

My process began by setting up npm for the project with the command:

npm init

I then proceeded to install hapi js using the command:

@hapi/hapi

Following that, I created the server.js file containing the code snippet below:

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

Upon running node server.js in the terminal and accessing localhost:4000 in the browser, I successfully see the "Hello World!" message.

I then proceeded with dockerizing the application. First, I created a Dockerfile containing the following instructions:

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

Furthermore, I created a docker-compose.yaml file with the following configuration:

version: '3.8'

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

After executing docker-compose up -d in the terminal and navigating to localhost:4000 in the browser, I encountered an error as shown in this image.

I am seeking guidance on identifying what is wrong with my code and how I can rectify it.

Answer №1

modify the host parameter to host: '0.0.0.0'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an NPM package based on Swagger documentation

We have established a NodeJs server and developed Swagger documentation. Now, we are interested in creating an NPM library client based on our Swagger documentation. We hope to provide this library for use in other NodeJS projects. Is there a swagger-cod ...

Optimize several projects or pages with RequireJS

How can I improve the efficiency of my multiple requirejs projects? For example, I have this structure below with two main.js in different locations/folders, build/ build.js dev/ index.php core/ js/ main.js libs/ ...

Trouble in paradise - the MEAN Stack Express server is facing technical

Running a Node.JS + Angular JS application on a cloud server using the MEAN stack has been proving challenging as the application keeps terminating every hour or sooner. I am in search of answers and would appreciate any insights into what could be causin ...

Headers cannot be set again after they have been sent to the client in Express Node

I attempted to create a post request for login with the following code: router.post('/login', async(req, res) =>{ const user = await User.findOne({gmail: req.body.gmail}) !user && res.status(404).json("user not matched") c ...

Currently utilizing an outdated JointsWP version

I am currently facing an issue with my older project that is running on JointsWP 4. For some reason, I am unable to run and compile it anymore. As this problem exceeds my current knowledge level, I have attempted to solve it by running npm install again, b ...

Issue: I am unable to locate the module '../../package.json' when attempting to run npm on the command line. Even running 'npm -v' results in an error message

Although I have come across similar questions, I am facing an issue with running anything using npm. Even a simple command like npm -v is throwing an error: C:\Users\Aloha>npm -v node:internal/modules/cjs/loader:1147 throw err; ^ Error: C ...

Error message: "The property 'ɵunwrapWritableSignal' is not found on the type 'typeof import_modules/@angular/core/core'". Here is how you can troubleshoot this issue in HTML files

Can anyone help me resolve this error that keeps appearing in all my Angular HTML pages? I am using Node version 14.17.4 and Angular version 15. The error message states: Property 'ɵunwrapWritableSignal' does not exist on type 'typeof impor ...

Remove route with problem: undefined parameters

I have encountered an error while working on a delete route that is causing issues with req.params.bookid. This is not a common problem for me. The console shows the error message: "TypeError: Cannot read property 'bookid' of undefined"... I atte ...

Node.js Ajax request failing with error code POST 404 (Not Found)

I've been attempting to utilize Ajax to retrieve data from the controller on the client-side every 5 seconds, but unfortunately, I encountered an error instead. Below is my controller code: queue2: function(req, res){ var mac = req.param('m ...

The POST request to the server comes back as a 404 error

I recently completed a project using React and Strapi headless CMS (implemented with nodejs). The backend, managed by Strapi, is hosted on port 443. Interestingly, while I receive valid responses when sending GET requests to any URL in the backend through ...

Access and retrieve pkpass files from a server using React

I've exhausted all options but still can't get it to work. I'm attempting to create an Apple wallet pass using https://github.com/walletpass/pass-js. When I download the pass on the node server where I've implemented it, everything work ...

When a node sends a request to an endpoint, it receives a response from

In my project, I have a file named "forms.routes.js" which contains a variety of endpoints using router.get, router.post, router.put, and router.delete. Interestingly, when I try to access the 16th endpoint in the list: localhost:3000/v2/forms/:domain/co ...

Utilizing Angular in combination with Express-Handlebars for a Node.js project

I'm currently working on a node.js application with Express and express handlebars as the templating framework. Recently, I've been delving into Angular and trying to integrate it into my node.js app. However, I've encountered an issue wher ...

What is the best way to retrieve information stored in objects with ejs?

I am currently passing data from my app.js file to my ejs files in this format: { _id: 2500, firstName: 'John', lastName: 'Doe', comments: [ { _id: 5555, title: "Hello", comment: "Hello, World" } ], __v: 0 } By using a forEach loop, I ...

What is the best way to create multiple iterations within a handlebars each loop?

Hey everyone, I require a nested 'each' inside another 'each' in Handlebars. {{#each daychecker}} <div> {{#each daychecker.image }} <h1>{{this.url}}</h1> {{/each}} </div> {{else}} <div class="card mx- ...

Node.js failing to link CSS and JS files

I am struggling with using middleware func to serve a static file in Node and Express. The issue I'm facing is that my CSS file is not linking properly with the HTML file. The error message I receive is: Refused to apply style from 'http://local ...

When you run 'npm install', it triggers a 'npm ERR! code E401' message to appear

My co-worker is having trouble running 'npm install' even though I can do it successfully on my machine. Her command starts fine and seems to be running well for a few minutes, but then it stops abruptly with this error: npm ERR! code E401 npm ...

issue with map function not populating data

models/docs.js is used to fetch the API URL from the backend server const doc = { baseUrl: window.location.href.includes("localhost") ? "http://localhost:1337" : "", getallDocs: async function getallDocs() { ...

Encountered a Stdout maxBuffer error during the execution of a Gulp task

Recently, as I was setting up a new Backbone project and utilizing Gulp for task automation, a frustrating issue arose. Whenever I ran my task involving Browserify and gulp-compressor, an error message mentioning "stdout maxBuffer" would appear. In my app. ...

The npm and node versions do not match the SSH environment

After setting up the server app in EC2 AWS, I logged into the SSH server in the terminal to check the versions of node and npm. Below are the versions I found: npm : 8.11 node : 17.9.1 Surprisingly, when trying CI/CD using Github Actions, I discovered tha ...