Score:1

Still confused why docker works when you make a process listen to 0.0.0.0 but not 127.0.0.1

ve flag
const hostname = '0.0.0.0'; // << This is where I'm confused
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

When I dockerize this app and run it in a container, hostname 0.0.0.0 works, but 127.0.0.1 doesn't work. I understand the reason why is because docker containers pretty much get their own IP.

So when I build and run the container when I set the hostname variable to 127.0.0.1, and then visit 127.0.0.1 on my browser, I'm not connecting to the container's IP address, but my local machine.

But why is it when I run the containerized app on 0.0.0.0 and visit 127.0.0.1 on my browser, it now connects to the container instead of my local machine?

Thank you.

Score:2
in flag

docker is "a different machine" and your machine gets a port forward to that machine on localhost.

So when the app inside docker listens to 127.0.0.1 that is only valid inside that machine, to connect to it from "outside" you need to listen to the any address.

So there is 2 different 127.0.0.1.

If you listen to any (0.0.0.0) then it is also available on 127.0.0.1, and all other interfaces/IPs on the machine.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.