'Docker run - connection was reset while the page was loading

I'm playing around with Docker and I've run into an issue whereby whenever I try to access my container, I receive a "The connection was reset" message. I've read various other posts talking about using a bind option. But what I'm finding more confusing is the fact that if I follow the "getting-started" tutorial from docker it can connect successfully.

The base Dockerfile from the "Getting-started" tutorial is as follows:

FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

It doesn't use EXPOSE anywhere to specify a port, but I know from looking in src/index.js this listens on port 3000

Now the docker file for my app looks like the following:

FROM node:12-alpine
WORKDIR /hapi
COPY . .
RUN yarn install --production
CMD ["node", "server.js"]

My server.js code is as follows: (literally lifted straight from their docs)

'use strict';

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

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        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();

As you can see my app is also setup to listen to port 3000.

When I run the "getting started" container I use:

docker run -dp 3001:3000 getting-started

I can then navigate to localhost:3001 and see the website running.

To run my "hapi" container I use:

docker run -dp 3002:3000 --name hapi hapitest

and navigate to localhost:3002 in a browser I see I get the connection was reset message.

Both containers build successfully using the following commands from their respective directories container the Dockerfile:

docker build -t getting-started . and docker build -t hapitest .

furthermore if I run the hapi app locally i.e. just going to the command line and type node server I can successfully visit the page in question at localhost:3000

I've not really use hapi before, but seen as I've lifted the example straight from the docs I don't think that should be the issue, but I'm also new to Docker. But is anyone able to shed some light on this, as every appears identical and I'm pulling my hair out.

My only other thought was it's something to do with my WORKDIR directive but as far as I can tell everything is correct as all my files reside within the same location, whereas the getting-started files are in a "src" directory as referenced by the "src/index.js" in their CMD directive.

I even tried adding an EXPOSE 3000 line in my Dockerfile but this had no effect



Solution 1:[1]

Use "0.0.0.0" instead of "localhost" for host property. This will allow Docker to expose the port properly.

It's also --host flag for Vite users

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 Eugene