Raja G
Raja G

Reputation: 6633

Unable to start node application inside docker

While creating my node app inside docker I am getting below error

docker container run -it --rm --volume $(pwd):/usr/src/app -p 7007:3000 sample-app-dev:latest
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"nodemon server.js\": executable f

My Dockerfile looks like below

FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["nodemon server.js"]

Package.json

{
  "name": "nodedocker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.4"
  }
}

server.js

const express = require( "express" );

const app = express();
const port = 3000;

app.get( "/", ( req, res ) => {
res.send( "Hello World from express JS!!" );
} );

const hobbies = [
"Swimming", "Diving", "Jogging", "Cooking", "Singing" ] ;

app.get("/hobbies", (req, res) => {
res.send(hobbies);
});

const famous_programming = [
"python", "java", "c", "c++", "JS" ] ;

app.get("/famous_programming", (req, res) => {
    message = famous_programming.includes("node")? "Yaaay, Node.js" : "Yup! Node.js is a framework from JS" ; 
    res.send(message);
});

app.listen( port, () => {
console.log( `Node JS started on http://localhost:${port}` )
} );


I am not sure what else I am missing here, any inputs greatly appreciated.

Thank you.

Upvotes: 1

Views: 416

Answers (1)

Paolo
Paolo

Reputation: 25989

You could configure the package to run nodemon server.js on start, then specify npm start in the entrypoint:

Dockerfile:

FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["npm", "start"]

package.json:

{
  "name": "nodedocker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.4"
  }
}

Test:

$ docker container run -it --rm q63534772
> [email protected] start /usr/src/app
> nodemon server.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Node JS started on http://localhost:3000

Upvotes: 1

Related Questions