How do I use nodemon with docker in my node.js application? - node.js

I am new to Docker and coding. I have already added it as dev-dependency but still I have to build the image every time I make a change to the code. I have tried looking this up but have not found a solution that is suitable/working because I am using process.json file.
My Dockerfile :
FROM node:12.14.1-alpine
# app name
ENV APP_NAME=mock-api
ENV WORK_DIR /deploy/${APP_NAME}
# Create app directory
RUN mkdir -p ${WORK_DIR} && \
chown node:node ${WORK_DIR}
RUN apk add --update gnupg
WORKDIR ${WORK_DIR}
COPY ["yarn.lock", "package.json", "./"]
RUN yarn global add pm2 && yarn install --prod --frozen-lockfile && yarn cache clean
COPY --chown=node:node . .
EXPOSE 3000
USER node
CMD ["pm2-runtime", "--no-daemon", "--raw", "process.json"]
Process.json :
{
"apps": [
{
"name": "mock-api",
"script": "./app.js"
}
]
}
package.json:
{
"name": "mock-api",
"version": "1.0.0",
"description": "Mock API for the testing environment",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "arorasannidhya#gmail.com",
"license": "ISC",
"dependencies": {
"koa": "2.12.0",
"koa-joi-router": "^6.0.2",
"koa-logger": "3.2.1",
"koa-router": "9.0.1",
"openpgp": "4.10.4",
"pm2": "^4.2.3",
"uuid": "^7.0.2"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}

Nodemon is the utility to run the node application (it watches file in the directory and when changed runs index.js or app.js (whatever is your root file))
It cannot be used to build a docker image, you will need to do something like this https://vsupalov.com/rebuilding-docker-image-development/#:~:text=In%20Conclusion,see%20the%20results%20right%20away!

Related

Dockerfile.dev: nodemon: Permission denied

Setting up my dev environment on Docker. I am running Nodejs v16.16.0.
I am using Nodemon and a docker volume to keep my dev work in sync with the docker container.
Dockerfile.dev
FROM node:alpine
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "dev"]
package.json
{
"name": "docker-example-1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index",
"dev": "nodemon index",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Kane Hooper <kanehooper#hotmail.com>",
"license": "MIT",
"dependencies": {
"express": "^4.18.1"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
I execute docker run -p 8080:3000 -v $(pwd):/usr/src/app -v /usr/src/app/node_modules --name nodeappcontainer nodeapp
I get the following error /tmp/dev6585779965.sh: line 2: nodemon: Permission denied
Any assistance on how to resolve this would be much appreciated.
The problem resolved when I bumped the version of nodemon from 2.0.18 down to 2.0.16.
I've raised an issue on the nodemon github repo.

A2Hosting: npm works in terminal, but not for cron worker

I am using A2hosting and trying to configure cron worker, but no luck yet.
I followed this article to install node and npm, which is basically list of commands below
cd ~
wget https://nodejs.org/dist/v12.9.1/node-v12.9.1-linux-x64.tar.xz
tar xvf node-v12.9.1-linux-x64.tar.xz
mv node-v12.9.1-linux-x64 nodejs
mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
I have tried multiple commands
npm run --prefix ~/cloudflare-upload-tool start which produces /bin/bash: npm: command not found
and
~/nodejs/bin/npm run --prefix ~/cloudflare-upload-tool start which produces /usr/bin/env: node: No such file or directory
Both commands run fine in terminal, could you tell what are my options to fix this?
package.json
{
"name": "cloudflare-upload-tool",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "env-cmd node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0",
"cloudflare": "^2.7.0",
"env-cmd": "^10.1.0",
"form-data": "^3.0.0",
"fs": "0.0.1-security",
"jsonwebtoken": "^8.5.1",
"path": "^0.12.7",
"tus-js-client": "^2.2.0"
}
}
Support told that this approach is not supported. Ended up creating node js app using UI, stopping it as we don't need to expose it and using command for cron worker below as suggested in other article
source /home/<USERNAME>/nodevenv/cloudflare_upload_tool/10/bin/activate && cd /home/<USERNAME>/cloudflare_upload_tool && npm run start

Can't Dockerize my simple typescript website

I am trying to get a Typescript project running in Docker. I created the Docker file based on a tutorial but when I run it I get:
/usr/local/bin/docker-entrypoint.sh: 8: exec: .: Permission denied
I am not sure how to diagnosis this problem.
I can enter the container with
docker run --rm -it d3ca97c88aec bash -il
and run the server with
npm start
I have no file called docker-entrypoint.sh and I didn't make one.
I STRONGLY FEEL THIS IS BECAUSE TYPESCRIPT MAKES A BUILD FOLDER AND THAT BUILD FOLDER IS PERMISSIONED DIFFERENTLY.
Dockerfile
FROM node:12
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
ENV COUCH_URL=NotSafeForStackOverflow
RUN npx tsc
EXPOSE 8080
CMD npx ts-node index.ts
{
"name": "express-react-app",
"version": "1.0.0",
"main": "index.js",
"repository": "",
"author": "",
"license": "",
"private": null,
"dependencies": {
"agentkeepalive": "^4.1.3",
"apollo-utilities": "^1.3.4",
"base64-arraybuffer": "^0.2.0",
"express": "^4.17.1",
"express-graphql": "^0.11.0",
"graphql": "^15.3.0",
"graphql-depth-limit": "^1.1.0",
"graphql-tag": "^2.11.0",
"graphql-type-json": "^0.3.2",
"i": "^0.3.6",
"merge-graphql-schemas": "^1.7.8",
"nano": "^8.2.2",
"node-webcrypto-ossl": "^2.1.1",
"npm": "^6.14.8",
"text-encoding": "^0.7.0",
"ts-node": "^9.0.0",
"typescript": "^3.7.4",
"uuid": "^8.3.0"
},
"scripts": {
"start": "npx ts-node index.ts",
"build": "npx tsc",
"dev": "nodemon --watch '**/*.ts' --exec 'ts-node' index.ts"
},
"devDependencies": {
"#types/express": "^4.17.7",
"#types/graphql": "^14.5.0",
"#types/node": "^14.6.3"
}
}
when you run CMD npx ts-node index.ts
it will overwrite default CMD [ "node" ]
please refer link below to how to use node image :
https://github.com/nodejs/docker-node/blob/master/README.md#how-to-use-this-image
you shoud be able done by using docker-compose :
version: "2"
services:
node:
image: "node:8"
user: "node"
working_dir: /home/node/app
environment:
- NODE_ENV=production
volumes:
- ./:/home/node/app
expose:
- "8081"
command: "npm start"

Docker - Puppeteer require somethings and wont run

I've created a container with Docker from a NodeJS app,
but when I try to run it the application will start but goes in error loop.
My Dockerfile contain
RUN npm install
but it continuously break on:
ERROR PuppeteerCrawler:PuppeteerPool: Browser launch failed {"id":12}
Error: Could not find browser revision 756035. Run "npm install" or "yarn install" to download a browser binary
If i run Node main.js there are no problem.
//Dockerfile
FROM apify/actor-node-basic
COPY package*.json ./
RUN npm --quiet set progress=false \ && npm install --only=prod
--no-optional \ && echo "Installed NPM packages:" \ && npm list \ && echo "Node.js version:" \ && node --version \ && echo "NPM version:" \ && npm --version
COPY . ./
WORKDIR .
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "Worker.js" ]
// package.json
{
"name": "SoccerBrain",
"version": "0.0.1",
"description": "This is an example of an Apify actor.",
"dependencies": {
"apify": "^0.21.0",
"puppeteer": "^5.0.0",
"rimraf": "^3.0.2"
},
"devDependencies": {},
"scripts": {
"start": "node Worker.js",
"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
},
"author": "It's not you it's me",
"license": "ISC"
}
what's append?

Dockerfile RUN doesn't to run in container context

I'm having an issue trying to generate a dockerfile for my nodejs app:
My dockerfile:
FROM node
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["node", "/app/index.js"]
The nodejs (As part of npm install) needs grpc. When I try to run my app, I get the following error message:
Cannot find module '/app/node_modules/grpc/src/node/extension_binary/node-v57-linux-x64/grpc_node.node'
When I explore the app/node_modules/grpc/src/node/extension_binary/ folder, node-v48-win32-x64 is the only folder inside there. My guess is when npm install ran, it used the context my host machine where it detected windows/x64 and downloaded that binary instead. I'd like to avoid running npm install at runtime. How do I fix this?
My package.json:
{
"name": "microservice-test",
"version": "1.0.0",
"description": "A test microservice.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "FrankerZ",
"license": "ISC",
"devDependencies": {
"grpcc": "0.0.8",
"gulp-livereload": "^3.8.1"
},
"dependencies": {
"async": "^2.5.0",
"grpc": "^1.6.0",
"gulp": "^3.9.1",
"gulp-run": "^1.7.1",
"gulp-util": "^3.0.8",
"protoc-plugin": "0.0.6"
}
}
What I think is happening is, docker run is copying the local node_modules from your project inside the container at COPY . /app
.
So thus you get the linux-x64 error. It basically copied all the machine specific code from node_modules to the container that must have another OS. To fix this, ignore node_modules by making a .dockerignore file alongside your package.json and add just one line.
node_modules
Read more about it from here.

Resources