Dockerizing Node.js app - node.js

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.

Related

NodeJS + Docker + OSX

I'm trying to run from Docker a simple "hello world" in NodeJS, following a video tutorial. This is my first attempt in nodejs and in Docker so I'm sure I miss something obvious but passing through the video compared to the code I wrote many times, I still can't find the error.
Locally it works fine, but in Docker is unable to run. In Docker Desktop the container becomes green and immediately gray again.
I've tried to change the path many times, thinking that the problem is there but without any clue. I really haven't the experience to investigate further, please help me to solve the issue.
My Dockerfile
FROM node:16
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["npm", "run", "dev"]
package.json
{
"dependencies": {
"express": "^4.18.1",
"node": "^18.5.0"
},
"name": "contratti",
"version": "1.0.0",
"description": "prova",
"main": "App.js",
"scripts": {
"start": "node App.js",
"dev": "nodemon -L App.js"
},
"author": "jegor",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.19"
}
}
App.js
const express = require("express");
const app = express();
app.get("/", (req,res) => {
res.send("<h2> Ciao ciao ..</h2>");
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server running at ${port}`));
ZSH
docker run -v $(pwd):/usr/src/app -p 3000:3000 -d --name contratti contratti
Error returned
> contratti#1.0.0 dev
> nodemon -L App.js
/usr/src/app/node_modules/.bin/node: 1: /usr/src/app/node_modules/.bin/node: �����: Input/output error
/usr/src/app/node_modules/.bin/node: 3: /usr/src/app/node_modules/.bin/node: Syntax error: Unterminated quoted string

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" ]

docker-compose up is not mapping port [duplicate]

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
});

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
});

Resources