0

While trying to dockerize node app, when I visit localhost:8000 I get this error:

The connection was reset - the connection to the server was reset while the page was loading.

In the terminal when I use the run command on image, I get the desired output in console. It says:

Server running at http://localhost:8000/

Dockerfile:

FROM node

RUN mkdir -p /app/
WORKDIR /app

COPY package.json /app

RUN cd /app

RUN npm install

COPY . /app

CMD ["node", "index.js"]
EXPOSE 8000

index.js:

#!/usr/bin/env nodejs

var http = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(8000, 'localhost');
console.log('Server running at http://localhost:8000/');

package.json:

{
  "name": "server1",
  "version": "1.0.0",
  "description": "Dockerizing node-app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Himanshu",
  "license": "ISC"
}

Here is the run command that I used

sudo docker run -p 8000:8000 -it --name node-container2 my-node-image

All these files are saved in same directory.

8
  • how do you run the container? Commented Oct 19, 2017 at 12:10
  • 1
    sudo docker run -p 8000:8000 -it --name node-container2 my-node-image Commented Oct 19, 2017 at 12:13
  • 1
    @Veve Okk I ll add it now Commented Oct 19, 2017 at 12:15
  • 1
    Yes I tried 0.0.0.0 It gives same result. And this is the only thing I am getting on running the container Server running at localhost:8000 Were u asking for this? Commented Oct 19, 2017 at 12:18
  • 1
    Yeah I did that and got this..... Server running at localhost:8000... Nothing else Commented Oct 19, 2017 at 12:33

1 Answer 1

1

Just change your index.js to work on 0.0.0.0 inside container:

#!/usr/bin/env nodejs

var http = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(8000, '0.0.0.0');
console.log('Server running at http://0.0.0.0:8000/');

And you will be able to access your app via localhost on a host machine.

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

1 Comment

I have accepted but votes casted by those with less than 15 reputations are not displayed publicaly. Sorry :( and thank you for the help.

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.