1

I try to run Docker. I created file server.js:

'use strict';

const express = require('express');

// константы
const port = 8080;
const host = '0.0.0.0';

// приложение
const app = express();
app.get('/', (req, res) => {
    res.send('Hello World');
});

app.listen(port, host);
console.log(`running on http://${host}:${port}`);

I created package.json and init package.lock by npm install

{
  "name": "docker",
  "version": "1.0.0",
  "description": "node.js on docker",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [
    "node",
    "docker"
  ],
  "author": "Molchanova Tatyana",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

and I created a Dockerfile

FROM node:12

# создание директории приложения
WORKDIR /usr/src/app

# установка зависимостей
# символ астериск ("*") используется для того чтобы по возможности
# скопировать оба файла: package.json и package-lock.json
COPY package*.json ./

RUN npm install
# Если вы создаете сборку для продакшн
# RUN npm ci --only=production

# копируем исходный код
COPY . .

# EXPOSE декларирует, что можем пробросить порт 8080, но на самом деле не пробрасывает
EXPOSE 8080
CMD [ "node", "server.js" ]

When I create iamge with: docker build -t hello-world .

it runs in console: running on http://0.0.0.0:8080

Then I try to run it with external and internal 8080 ports: docker run --rm --name web -p 8080:8080 hello-world

And when I go by link http://0.0.0.0:8080 - the page is empty, no access to site And page is not accessible not only with docker run, but when I just run server.js with the command in terminal - it's not accessible.

Only when I point localhost

const host = '127.0.0.1';

instead of

const host = '0.0.0.0';

in server.js file it runs in terminal but not working in browser with running as Dockerfile - docker run --rm --name web -p 8080:8080 hello-world

Please, help!

In hosts file all is ok, no 0.0.0.0

1 Answer 1

2

Preamble

Docker container should be considered as a separate node, which has no connection with host (machine where Docker is running on). Host and containers are communicating via network, so each has its own network interfaces. And every machine connected to some network should have an IP address, so it can be accessed.

When the container is created, Docker assigns some internal IP address to this container and uses it to forward requests. When you're running a container with an options --name web -p 8080:8080, Docker will forward requests from 127.0.0.1:8080 of host machine to IP address of web container and port 8080 of this container.

TL;DR

You should use const host = '0.0.0.0'; in your docker image, but access your application by http://127.0.0.1:8080 from host.

What is 127.0.0.1

127.0.0.1 is a reserved address for loopback interface, which basically transfers the traffic to the same network node and this interface is not reachable from outside the node.

So, when you're setting const host = '127.0.0.1', you're making your application accessible only from this docker container by URL http://127.0.0.1:8080.

What is 0.0.0.0

0.0.0.0 is also a reserved address that stays for "all IP addresses on the local machine" when used as a listening IP address. So, when you're creating a listener for 0.0.0.0 it will serve all requests regardless of which IP address was assigned to the container.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot! It works on 127.0.0.1:8080!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.