0

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!

theKid
  • 522
  • 6
  • 19
  • What is `app`? If the service is unreachable from outside its own container and it prints a message `listening at 127.0.0.1` that's typically a sign you need to reconfigure something to listen on the special `0.0.0.0` "all interfaces" address instead. Express by default listens everywhere but other frameworks may behave differently. – David Maze Mar 31 '22 at 22:24

1 Answers1

3

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

Bad_Coder
  • 1,019
  • 1
  • 20
  • 38
  • Hey! This worked pretty well. Now my question is, why this? Is there a "prettier" way to add it as before? Like `server.listen(process.env.PORT || 3000);`? And can you explain why it worked using `0.0.0.0`? Thanks! – theKid Mar 31 '22 at 23:18
  • 1
    i'm no expert on node.js but perhaps this should work`server.listen(process.env.PORT || 3000, '0.0.0.0');` Also on why it works with `0.0.0.0` is explained better there https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip – Bad_Coder Mar 31 '22 at 23:31
  • 1
    localhost (127.0.0.1) and 0.0.0.0 has different meanings – Bad_Coder Mar 31 '22 at 23:32
  • in my case `app= fastify() sever.listen(3000)` server is not run. – Ruthe Dec 23 '22 at 07:07