On server startup NPM find start script - node.js

I'm working with docker and I'm wondering how I can get the command npm start to locate the app.js file without me doing it via the command line.
my package.json (located: /srv/www) looks as so:
{
"name": "dist",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "forever start -c \"nodemon --harmony\" app.js --exitcrash"
},
"author": "",
"license": "ISC"
}
I'm currently invoking my docker image as so:
docker run -d -v /srv/docker/instantynode/srv:/srv -p 80:8080 myimg ???
I am hoping to replace the ??? with a command which will startup node and invoke npm start in the correct directory, any ideas?
I was thinking maybe of making a little startup script to fix this however I was wondering if npm can fix this on it's own?

Regarding node application, you can start with ONBUILD in Dockerfile, so you can try as below:
$ cat Dockerfile
FROM node:0.10-onbuild (or version 0.12, depend your request)
PORT 8000
the image node:0.10-onbuild has codes already as below:
WORKDIR /usr/src/app
ONBUILD COPY package.json /usr/src/app/
ONBUILD RUN npm install
ONBUILD COPY . /usr/src/app
CMD ["npm" "start"]
With ONBUILD, during docker build, your node.js (or angularjs) codes will be not copied to image and npm install doesn't run.
But when you run the container, it starts to copy the files to /usr/src/app and install npm package. Then start npm service.
So in your case, you should be fine to run your application directly without mount the volume every time.
# I guess file package.json is under /srv/docker/instantynode/srv
$ cd /srv/docker/instantynode/srv
$ cat Dockerfile
FROM node:0.10-onbuild
PORT 8000
$ docker build -t myimg .
$ docker run -d -p 8000:8000 myimg
That's all, you should be fine to access your application via port 8000 now.

Related

Docker container is refusing connection

I am using Dockerfile for Nodejs project but its returning Connection refused error
When I run the app without Docker it works absolutely fine.
Command I use to build and run the docker container is as follows:
docker build -t myapp .
docker run -it -p 8080:8080 myapp
Running above command runs without any error but when I hit http://localhost:8080/test-url it fails
My dockerfile is as follows:
FROM node:16.16.0-alpine
ADD . /opt
COPY . .
RUN npm install
EXPOSE 8080
RUN chmod +x /opt/deploy.sh
RUN apk update && apk add bash
CMD ["/bin/bash", "/opt/deploy.sh"]
And my package.json is as follows (truncated to show only script):
"scripts": {
"start": "DEBUG=app* node index.js",
"build": "rimraf build && babel-node ./src --out-dir build/src && npm run docs",
"dev": "DEBUG=app* nodemon --exec babel-node index.js",
"lint": "eslint 'index.js' 'src/**/*.js' 'src/index.js'",
"docs": "apidoc -i src/ -o public/docs",
"prepare": "husky install",
"lint-staged": "lint-staged"
},
For development I use following command which works fine::
npm run dev
For deploymeent I run deploy.sh which has env variables and final command as ::
npm run build
npm run start
Even when I am trying http://localhost:8080/test-url by loging into docker interactive terminal it returns same error - Connection Refused
Your Port mapping looks right to me. Did you check your firewall? Maybe it is blocking the connection. It could also be helpful to test, if you can run an NGINX-Container on Port 8080. That way you can check if it is a general configuration-problem with Docker or a specific problem of your Image.
Also, did you try to set your node server to listen to 0.0.0.0 instead of localhost? I'm not sure how Docker handles IPs in the Containers, but I was thinking maybe it is called by it's internal ip. If that is the case and your server listens to localhost only, it shouldn't accept the connection.
I hope one of these things can point you in the right direction.

Docker Node backend - "this site can't be reached"

I'm trying do build a docker image of my Node backend for deployment but when I run it in a container and open in the browser I get "This site can’t be reached" error and the following log in dev tools:
crbug/1173575, non-JS module files deprecated
My backend is based on GraphQL Apollo server. Dockerfile is as following:
FROM node:16
WORKDIR /app
COPY ./package*.json ./
RUN npm ci --only=production
# RUN npm install
COPY . .
# RUN npm run build
EXPOSE 4000
CMD [ "node", "dist/main.js" ]
I've also tried to use the commented code, with no result.
The image builds without a problem and after running the container I get 🚀 Server ready at localhost:4000 in the docker logs, so I'd expect it to work properly.
"scripts": {
"build": "tsc",
"start": "node dist/main.js",
"dev": "concurrently \"tsc -w\" \"nodemon dist/main.js\""
},
That's the scripts part of my package.json I've also tried CMD ["npm", "start"] in Dockerfile but that doesn't work either. When I run the backend from terminal using npm start I can access the GraphQL playground at localhost:4000 - I assume that should be the same with docker?
I'm still new to docker so I'd be grateful for any hints. Thanks
EDIT:
I run the container with the following command:
docker run --rm -d -p 4000:80 image-name:latest
Seemingly it's running on port 0.0.0.0:4000 as that's what it says under 'PORT' when I execute docker ps
Please run docker inspect command and you will get IP and then run through that ip in browser

Docker typescript node.js can't start app?

I have an index.ts, then this is my script
"scripts": {
"test": "jest",
"start": "ts-node-dev ./index.ts"
},
I tried to dockerize it, what should I do? do I need to add another command for npm build? or generate a .js file?
my dockerfile like this
FROM node:10-alpine
WORKDIR /
# copy configs to /app folder
COPY package*.json ./
COPY tsconfig.json ./
COPY . .
# check files list
RUN ls -a
RUN npm install
EXPOSE 3001
CMD [ "npm", "start"]
I can't access localhost:3001 in my browser after I run
docker build -t testApp .
then
docker run -p 80:3001 testApp

Docker + React App - Docker run command doesn't open up localhost

I'm having trouble with running Docker (I'm a total beginner at it) with my React app. This was given as a coding challenge so I was given these Docker commands:
docker build --tag app-name .
docker run -it -p 7357:80 app-name
Here is my dockerfile:
FROM node:6-wheezy
WORKDIR /srv/tenor-frontend-test/
COPY package.json package-lock.json ./
RUN npm install
COPY . .
EXPOSE 80
ENV NODE_JS_PORT=80
CMD [ "npm", "start" ]
My start script in package.json:
"start": "PORT=7357 react-scripts start"
Using "npm start" in my terminal opens up localhost fine but when I run the "docker run" command, it prints exactly what happens with "npm start" but no browser opens and localhost:7357 doesn't work.
I'm hoping this is just a dockerfile issue since "npm start" works fine?
Thanks in advance!

How to add npm packages to docker images w/o remaking image or container?

Question:
How can I install new packages and get docker image and container to recognize change, install and rerun w/o working developer interaction?
package.json: notice that we have only 1 package
"scripts": {
"start": "node_modules/.bin/nodemon"
},
"dependencies": {
"express": "4.15.4"
}
dockerfile
FROM node:8.4.0
ENV HOME /var/www
WORKDIR ${HOME}
COPY server/package.json server/tsconfig.json server/nodemon.json $HOME/
RUN npm install
EXPOSE 8191
CMD [ "npm", "start" ]
Make image:
docker build . -t testImage
Run container:
docker run -p 8191:8191 -v $(pwd)/server/src:/var/www/src testImage
Everything works as intended [nodemon] startingnode ./src/index.js`
The Problem:
npm install cors --save will add cors package
"scripts": {
"start": "node_modules/.bin/nodemon"
},
"dependencies": {
"express": "4.15.4",
"cors": "^2.8.4",
}
Problem is nothing will update in my docker container :(
I do not want the developer to have to remake the image or to have to rerun the a container:
docker build . -t testImage
docker run -p 8191:8191 -v $(pwd)/server/src:/var/www/src testImage
Take a look how ENTRYPOINT AND CMD works, if you want to use the same Dockerfile for production and development you can create a entrypoint script that if no arguments is provided run as production mode, if argument like development, run as development mode.
save development packages with --save-dev, so when running in development mode your container will install the dev dependencies.
Make sure your node_modules is at your .dockerignore,
Your dockerfile:
FROM node:8.4.0
ENV HOME /var/www
WORKDIR ${HOME}
COPY start_scrip.sh /start_scrip.sh
COPY server/package.json server/tsconfig.json server/nodemon.json $HOME/
RUN npm install
EXPOSE 8191
ENTRYPOINT ["/start_scrip.sh"]
A start_scrip.sh example:
#!/usr/bin/env bash
function development {
npm install --dev #
npm start
}
function run_prod {
#npm start command here
}
if [ $# -gt 0 ]
then
if [ $1 == "development" ]
then
development
else
echo "Not a valid argument"
exit 127
fi
else
run_prod
fi
exit 0
Make sure to chmod +x your script, so it can be executed inside the container.
To run in development mode:
run -p 8191:8191 -v $(pwd)/server/src:/var/www/src testImage development
Every time your developer add a new package in the workstation he should restart the container.

Resources