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.
Related
I am trying to setup Nodemon in a Docker container. It says that nodemon is running, but when I change code in my index.js file it does not reload like it does outside of docker. I've tried adding -L to the command, but no luck. I've also tried installing nodemon in the docker file instead, but no luck.
I have to do docker-compose up --build anytime I change my index.js file.
Any ideas?
Here is my file structure:
-api
-node_modules
-.dockerignore
-Dockerfile
-index.js
-package.json
-package-lock.json
-docker-compose.yml
docker-compose.yml:
version: '3.4'
services:
api:
build:
context: ./api
container_name: api
environment:
- PORT=3001
volumes:
- ./api/src:/usr/app/src
ports:
- '3001:3001'
command: npm run dev
Dockerfile:
FROM node:14.15.2-alpine3.12
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
package.json:
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"dev": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.6"
}
}
I FIGURED IT OUT!!!
After a lot of trial and error. It has to do with my volumes in my docker compose as well as nodemon. Not 100% sure why any insight would be helpful too.
The fix was to change my volume from
- ./api/src:/usr/app/src
to:
- ./api:/usr/src/app
Then I had to add the -L flag to my nodemon command in order for it to reload.
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!
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"
I'm working with NodeJS and Nodemon on Docker. When I try to run my NodeJS app using nodemon command directly in docker compose file, it runs.
Like this (working): [docker-compose]
command: nodemon source/index.js
But when I use a script from package.json, it doesn't work
Like this (not-working): [docker-compose]
command: npm run dev
Where my package.json file is
"scripts": {
"start": "node source/index.js",
"dev": "nodemon source/index.js"
}
I tried different things, when I simply run start script without nodemon, it works
Like this (working): [docker-compose]
command: npm run start
But when I try to use dev again with nodemon command inside it, it doesn't work. Container won't start. I have also tried the following and it also works
Like this (working): [docker-compose]
command: nodemon --exec npm start
I still don't understand, why nodemon command is not working inside script dev
I'm using Docker in Swarm Mode
Here are my both files
docker-compose
version: '3.7'
services:
node-service:
image: node-img:1.0
ports:
- 4000:4000
working_dir: "/node-dir"
volumes:
- ./node-dir/source:/node-dir/source
networks:
- ness-net
command: npm run dev
networks:
ness-net:
package.json
{
"name": "node-pkg",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node source/index.js",
"dev": "nodemon source/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^1.19.4"
}
}
Just add the "." to define the path in your package.json like this
"scripts": {
"start": "node ./source/index.js",
"dev": "nodemon ./source/index.js"
}
You need to add an environment variable to point on npm when running nodemon
C:\........\npm the path should be like this , and choose a name
Try this solution:
services:
node-app:
container_name: node-app
image: node:latest
restart: always
volumes:
- ./node/source:home/node/source
working_dir: /home/node/source
ports:
- 4000:4000
networks:
- main-network
command: "tail -f /dev/null && npm start"
depends_on:
- db
logging:
driver: "json-file"
options:
max-file: "4"
max-size: "100m
Here is package.json
"main": "index.js",
"scripts": {
"preinstall": "npm i nodemon -g",
"start": "nodemon index.js",
}
Please make sure there should be index.js and package.json in working directory.
I'm starting a project in Vue.JS and I'm a little new to nodemon.
Here is my package.json file
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run eslint'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I can get nodemon to run through app.js with "nodemon src/app.js". I've tried a whole bunch of combinations after --exec and haven't had any luck.
The correct way is (in package.json and windows):
"scripts": {
"start": "node index",
"start-dev": "nodemon --exec \"npm run lint && node index\"",
},
This works pretty fine for your use-case.
nodemon src/app.js --exec "npm run lint && node"
or you can write nodemon.json file in your root directory
{
"watch": ["src"],
"ignore": ["**/*.test.js", "**/*.spec.js", ".git", "node_modules"],
"exec": "npm run lint && node src/app.js"
}
Im using nodemon version 1.19.4. You just missed the "events" key. The right way would be to create a nodemon.json in your root folder like that, then a lint script in your package.json with your lint command:
{
"watch": [ "src" ],
"ignore": ["**/*.test.js", "**/*.spec.js", ".git", "node_modules"],
"events": {
"restart": "npm run lint"
}
}
Here you can check about Nodemon Events.
When using events you don't need to manually handle your application state (restart, crash, node execution, etc), just put what you want to happen when nodemon refreshes.
I've been using a custom script for a while now which I finally published to npm.
Check it out here: https://github.com/theoephraim/lint-fix-nodemon
This helps avoid double restarts when eslint fixes your files as well as not failing on the initial run if eslint has fatal errors.
Hope it helps!