Home > Mobile >  localhost didn’t send any data on Docker and Nodejs app
localhost didn’t send any data on Docker and Nodejs app

Time:04-01

I've searched this answer on the StackOverflow community and none of them resulted so I ask one here.

I have a pretty simple nodejs app that has a server.js file, having the following.

    'use strict'
    require('dotenv').config();
    const app = require('./app/app');
    
    const main = async () => {
        try {
    
            const server = await app.build({
                logger: true,
                shopify: './Shopify',
                shopifyToken: process.env.SHOPIFY_TOKEN,
                shopifyUrl: process.env.SHOPIFY_URL
            });
    
    
            await server.listen(process.env.PORT || 3000);
    
    
        } catch (err) {
            console.log(err)
            

process.exit(1)
    }
}
main();

If I boot the server locally works perfect and I able to see a json on the web browser.

Log of the working server when running locally:

{"level":30,"time":1648676240097,"pid":40331,"hostname":"Erick-Macbook-Air.local","msg":"Server listening at http://127.0.0.1:3000"}

When I run my container, and I go to localhost:3000 I see a blank page with the error message:

This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

I have my Dockerfile like this:

FROM node:16

WORKDIR /app

COPY package.json .

RUN npm install

COPY . ./

EXPOSE 3000

CMD ["node", "server.js"]

This is how I run my container: docker run -d -it --name proxyservice -p 3000:3000 proxyserver:1.0

And when I run it I see the container log working:

{"level":30,"time":1648758470430,"pid":1,"hostname":"03f5d00d762b","msg":"Server listening at http://127.0.0.1:3000"}

As you can see it boot's up right, but when going to localhost:3000 I see that error message. Any idea of what am I missing/doing wrong?

Thanks!

CodePudding user response:

can you add 0.0.0.0 in the host section of your service, something like this?

server.listen(3000, '0.0.0.0');

give it a try then. Since you want your service to be accessible from outside the container you should give the address as 0.0.0.0

  • Related