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
Related
Having some problems while trying to create my first node.js app , super new to JS..
Trying to dockerize the app , like so :
docker build -t echo_app .
docker run -p 3000:3000 echo_app
End goal is to echo user input , like so :
http://example/?name=Eyal -> Hello Eyal
http://example/ -> Hello World
ERROR IM GETTING
Error: Cannot find module 'express'
Require stack:
- /app/index.js
.
.
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/index.js' ]
}
Directroy containes :
index.js
const express = require('express')
const log4js = require('log4js')
const app = express()
const logger = log4js.getLogger()
const echo = (req, res) => {
logger.debug("Request: ", req)
const input = 'name' in req.query ? req.query.input : ''
if (input.length == 0) {
res.send('Echo World')
} else {
res.send(`Echo ${input}`)
}
}
app.get('/', (req, res) => echo(req, res))
Dockerfile
FROM mhart/alpine-node:12
WORKDIR /app
ADD . ./
ENTRYPOINT ["node", "/app/index.js"]
package.json
{
"name": "echo",
"version": "1.0.0",
"description": "You talk, we talk back!",
"main": "index.js",
"author": "eyal",
"license": "MIT",
"dependencies": {
"express": "^4.17.1",
"js-yaml": "^3.13.1",
"log4js": "^5.2.2",
"saslprep": "^1.0.3"
}
}
To get it up and running, you first need to install the node dependencies by adding npm install to your Dockerfile, like this
FROM mhart/alpine-node:12
WORKDIR /app
ADD . ./
RUN npm install
ENTRYPOINT ["node", "/app/index.js"]
Then you need to have your Node app listen for requests by adding
app.listen(3000, () => {
console.log(`Example app listening at http://localhost:3000`)
})
at the bottom of index.js.
Finally, a small error in your code. req.query.input needs to be req.query.name.
That should hopefully get you going.
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" ]
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
});
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
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
});