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