Reputation: 36
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.
Upvotes: 0
Views: 209
Reputation: 28593
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.
Upvotes: 1