Docker /bin/bash: nodemon: command not found
Solution 1
The issue has been resolved. The issue boiled down to me not having node_modules
in the mounted volume.
Basically, while doing docker-compose build
the image was build correctly with the actual code being added to the image and creating the node_modules
folder by npm install
in the project root. But with docker-compose up
the code was being mounted in the project root and it was overriding the earlier added code including the newly created node_modules
folder.
So as a solution I compromised to install nodejs on my host and do a npm install
on my host. So when the code my being mounted I still got my node_modules
folder in my project root because it was also getting mounted from my host.
Not a very elegant solution but since it is a development setup I am ready for the compromise. On production I would be setting up using docker build
and docker run
and won't be using nodemon
anyways.
If anyone can suggest me a better solution I will be greatful.
Thanks!!
Solution 2
I believe you should use a preinstall script in your package.json.
So, in the script section, just add script:
"scritpts": {
"preinstall": "npm i nodemon -g",
"start": "nodemon app.js",
}
And you should good to go :)
Solution 3
You need to set the node_modules as a mounted volume in the docker container.
e.g
docker-compose.yml
app:
build: .
command: make run
volumes:
- .:/test
- /test/node_modules
environment:
NODE_ENV: dev
ports:
- "17883:17883"
- "17884:17884"
Solution 4
Pretty late for an answer. But you could use something called as named volumes to mount your node_modules in the docker volumes space. That way it would hide your bind mount.
Surender Thakran
Updated on July 05, 2022Comments
-
Surender Thakran almost 2 years
I am trying to mount my working node code from my host into a docker container and run it using nodemon using docker-compose. But container doesn't seems to be able to find nodemon. Note: My host machine does not has node or npm installed on it.
Here are the files in the root folder of my project (test). (This is only a rough draft)
Dockerfile
FROM surenderthakran/nodejs:v4 ADD . /test WORKDIR /test RUN make install CMD make run
Makefile
SHELL:=/bin/bash PWD:=$(shell pwd) export PATH:= $(PWD)/node_modules/.bin:$(PWD)/bin:$(PATH) DOCKER:=$(shell grep docker /proc/1/cgroup) install: @echo Running make install...... @npm config set unsafe-perm true @npm install run: @echo Running make run...... # Check if we are inside docker container ifdef DOCKER @echo We are dockerized!! :D @nodemon index.js else @nodemon index.js endif .PHONY: install run
docker-compose.yml
app: build: . command: make run volumes: - .:/test environment: NODE_ENV: dev ports: - "17883:17883" - "17884:17884"
package.json
{ "name": "test", "version": "1.0.0", "description": "test", "main": "index.js", "dependencies": { "express": "^4.13.3", "nodemon": "^1.8.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "api", "nodejs", "express" ], "author": "test", "license": "ISC" }
index.js
'use strict'; var express = require('express');
I build my image using
docker-compose build
. It finishes successfully. But when I try to run it usingdocker-compose up
, I get:Creating test_app_1... Attaching to test_app_1 app_1 | Running make run...... app_1 | We are dockerized!! :D app_1 | /bin/bash: nodemon: command not found app_1 | make: *** [run] Error 127 test_app_1 exited with code 2 Gracefully stopping... (press Ctrl+C again to force)
Can anyone please advice?
Note: The Dockerfile for my base image
surenderthakran/nodejs:v4
can be found here: https://github.com/surenderthakran/dockerfile_nodejs/blob/master/Dockerfile -
dnephin over 8 yearsI'm not that familiar with npm/node, but I think you could install them to a different url outside of the project root using
npm install -g
and then link to them withnpm link
instead of installing them again locally? Another alternative might be to install them to a custom path in the container usingnpm config set prefix
, then have a symlink in your project directory that points./node_modules
at that directory. I think that will work with both add and volumes. -
ViniciusArruda over 3 yearsMaybe you'll need to
RUN npm set unsafe-perm true
beforeRUN npm install
. -
Rafik Farhad about 3 yearsBullseye solution.