docker-compose up is not mapping port [duplicate] - node.js

It appears that my Hapi app is running in a Docker container, but I can't hit it in the browser. I thought that docker run -d -p 8080:3000 would have done it, but I guess not. I'm running boot to docker and neither http://localhost:8080/hello nor http://192.168.99.100:8080/hello is working.
I've tried tons of variations on this as well.
This is what I see when I run docker inspect <container id>:
Server running at: http://localhost:8080
Here's my Hapi.js server:
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 3000
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, h) {
return 'hello world';
}
});
async function start() {
try {
await server.start();
}
catch (err) {
console.log(err);
process.exit(1);
}
console.log(`App running at: ${server.info.uri}/hello`);
}
start();
Here's my Dockerfile:
FROM node:8.9.3
MAINTAINER My Name <email#email.com>
ENV NODE_ENV=production
ENV PORT=3000
ENV user node
WORKDIR /var/www
COPY package.json yarn.lock ./
RUN cd /var/www && yarn
COPY . .
EXPOSE $PORT
ENTRYPOINT ["yarn", "start"]
Here's my package.json:
{
"name": "my-app",
"version": "1.0.0",
"repository": "https://github.com/myname/myrepo.git",
"author": "My Name",
"license": "MIT",
"private": true,
"dependencies": {
"hapi": "17.2.0"
},
"scripts": {
"start": "node ./src/server"
}
}

The issue is not with Docker but how you configure the node server.
If you bind to localhost it will only be available from within the docker container. If you want to allow connections from the docker host either don't provide a hostname or use 0.0.0.0.
const server = Hapi.server({
host: '0.0.0.0',
port: 3000
});

Related

Add a simple React form application to an existing Nodejs docker image

I have created a Dockerfile, when run with docker-compose displays 'Hello world' in the browser. Now I want to be able to display a simple react form with some fields to enter text in and a submit button. Is there examples of any simple projects out there that I can just use. How will I update my files, that I have shown below
These are the files that I cam using:
Package.json -
{
"name": "nodejs-hello",
"version": "1.0.0",
"description": "des",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "hh",
"license": "ISC",
"keywords": [
"h"
],
"dependencies": {
"express": "^4.17.1"
}
}
index.js -
//now it load express module with `require` directive
var express = require('express')
var app = express()
const PORT = 8080;
//Define request response in root URL (/) and response with text Hello World!
app.get('/', function (req, res) {
res.send('Hello World')
})
//Launch listening server on port 8080 and consoles the log.
app.listen(PORT, function () {
console.log('app listening on port ' + PORT)
})
Dockerfile -
FROM node:latest
WORKDIR /app
COPY package.json index.js ./
RUN npm install
EXPOSE 8080
CMD node index.js
Docker Compose -
version: "3"
services:
web:
image: my-image:1.0
build: .
ports:
- '8080:8080'
https://medium.com/faun/a-simple-docker-setup-for-simple-hello-world-nodejs-application-bcf79bb608a0
What I have now is similar to the example in that link.
Now I just want to be able to display a react form in the browser.
Just create your React app and then add this line to copy the src files into the container:
COPY . .
Also, change the CMD (if you're using a CRA) to this:
CMD [ "npm", "start" ]

Why is container exiting immediately after executing docker run command?

I am running docker for the first time and my container is exiting immediately after docker run command. The docker ps is showing me an empty list. When I run docker ps -a I am getting the results of all the containers I pushed in Exited state.
I have tried using -ti command but the container is going in exit state.
I am using following commands to run the file: $ sudo docker build -t test_api3 . and $ sudo docker run -p 8080:3000 -ti test_api3 or ($ sudo docker run -p 8080:3000 -d test_api3)
Dockerfile
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm","start" ]
package.json
{
"name": "firstapp",
"version": "1.0.0",
"description": "first demo app",
"main": "http-server.js",
"scripts": {
"start": "node http-server.js"
},
"keywords": [
"S"
],
"author": "test",
"license": "ISC"
}
http-server.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I am running ubuntu os and it works (Let me know your setup if it still cannot work). The only problem with the code is that you cannot use the address 127.0.0.1 and must use 0.0.0.0 in your http-server.js. For more explanation refer to the link below
https://forums.docker.com/t/network-param-for-connecting-to-127-0-0-1-in-container/2333

Node Docker Runs, but can't see the application

It appears that my Hapi app is running in a Docker container, but I can't hit it in the browser. I thought that docker run -d -p 8080:3000 would have done it, but I guess not. I'm running boot to docker and neither http://localhost:8080/hello nor http://192.168.99.100:8080/hello is working.
I've tried tons of variations on this as well.
This is what I see when I run docker inspect <container id>:
Server running at: http://localhost:8080
Here's my Hapi.js server:
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 3000
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, h) {
return 'hello world';
}
});
async function start() {
try {
await server.start();
}
catch (err) {
console.log(err);
process.exit(1);
}
console.log(`App running at: ${server.info.uri}/hello`);
}
start();
Here's my Dockerfile:
FROM node:8.9.3
MAINTAINER My Name <email#email.com>
ENV NODE_ENV=production
ENV PORT=3000
ENV user node
WORKDIR /var/www
COPY package.json yarn.lock ./
RUN cd /var/www && yarn
COPY . .
EXPOSE $PORT
ENTRYPOINT ["yarn", "start"]
Here's my package.json:
{
"name": "my-app",
"version": "1.0.0",
"repository": "https://github.com/myname/myrepo.git",
"author": "My Name",
"license": "MIT",
"private": true,
"dependencies": {
"hapi": "17.2.0"
},
"scripts": {
"start": "node ./src/server"
}
}
The issue is not with Docker but how you configure the node server.
If you bind to localhost it will only be available from within the docker container. If you want to allow connections from the docker host either don't provide a hostname or use 0.0.0.0.
const server = Hapi.server({
host: '0.0.0.0',
port: 3000
});

Dockerizing Node.js app

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.
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.

Nodejs application not running by using docker compose

I'm dockerizing nodejs and mongoDB application but its not executing into browser.
URL is http://0.0.0.0:3030/
Error in browser:
This site can’t be reached
The connection was reset.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_RESET
By executing this command:
docker-compose up --build
It will generates following output into ubuntu terminal.
web_1 | npm info lifecycle test#1.0.0~prestart: test#1.0.0
web_1 | npm info lifecycle test#1.0.0~start: test#1.0.0
web_1 |
web_1 | > test#1.0.0 start /usr/src/app
web_1 | > node app.js
web_1 |
web_1 | App listning on port 3030
mongoDB_1 | 2017-05-16T07:07:27.891+0000 I NETWORK [initandlisten] connection accepted from 172.19.0.3:57655 #3 (1 connection now open)
Dockerfile
FROM node:alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN npm install nodemon -g
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
EXPOSE 3030
CMD [ "npm", "start" ]
docker-compose.yml
version: "2.0"
services:
web:
build: .
command: npm start
ports:
- "3030:3000"
volumes:
- .:/api
links:
- mongoDB
mongoDB:
image: mongo:3.0
ports:
- "27017:27017"
volumes:
- /srv/docker/mongodb:/var/lib/mongodb
restart: always
package.json
{
"name": "test",
"version": "1.0.0",
"description": "My first node docker application",
"main": "index.js",
"dependencies": {
"express": "^4.15.2",
"mongodb": "^2.2.26"
},
"devDependencies": {},
"scripts": {
"start": "node app.js"
},
"author": "Muzammil",
"license": "ISC"
}
Nodejs app.js
'use strict';
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const port = 3030;
let DB;
MongoClient.connect("mongodb://mongoDB/testDB", (err, db) => {
console.log(err);
//console.log(db);
DB = db;
});
app.get('/', (req, res)=>{
DB.collection("user").find({}, (err, result) => {
if(err) {
return res.json({message: "Error", error: err});
}
let results = [];
result.each((err, doc) => {
if(doc) {
console.log(doc);
}else {
res.end();
}
})
//res.status(200).json({message: "Successfully", code: 200, data: result});
});
});
app.listen(port, ()=>{
console.log(`App listning on port ${port}`);
});
You have - "3030:3000" in your docker-compose file, which means you're trying to bind localhost port 3030 to the container port 3000. So, either expose port 3000, or change the line to - "3030:3030"

Resources