When I start a dockerized Node.js testapp with
sudo docker-compose up
I get the following error:
Starting testapp_web_1 ... done
Attaching to testapp_web_1
web_1 |
web_1 | > testapp#0.0.1 start /usr/app
web_1 | > node index.js
web_1 |
web_1 | internal/modules/cjs/loader.js:613
web_1 | throw err;
web_1 | ^
web_1 |
web_1 | Error: Cannot find module 'mongodb'
web_1 | Require stack:
web_1 | - /usr/app/index.js
web_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:526:27)
web_1 | at Module.require (internal/modules/cjs/loader.js:666:19)
web_1 | at require (internal/modules/cjs/helpers.js:16:16)
web_1 | at Object.<anonymous> (/usr/app/index.js:4:21)
web_1 | at Module._compile (internal/modules/cjs/loader.js:759:30)
web_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
web_1 | at Module.load (internal/modules/cjs/loader.js:628:32)
web_1 | at Function.Module._load (internal/modules/cjs/loader.js:555:12)
web_1 | at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
web_1 | npm ERR! code ELIFECYCLE
web_1 | npm ERR! errno 1
web_1 | npm ERR! testapp#0.0.1 start: `node index.js`
web_1 | npm ERR! Exit status 1
web_1 | npm ERR!
web_1 | npm ERR! Failed at the testapp#0.0.1 start script.
web_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1 |
web_1 | npm ERR! A complete log of this run can be found in:
web_1 | npm ERR! /root/.npm/_logs/2019-05-04T15_34_04_615Z-debug.log
testapp_web_1 exited with code 1
The project structur is as follows
- testapp
--- docker-compose.yml
--- dockerfile
--- src
----- index.js
----- package.json
index.js
'use strict';
const MongoClient = require('mongodb').MongoClient;
const express = require('express');
// Constants
const PORT = 8080;
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.listen(PORT);
console.log(`Running on Port:${PORT}`);
package.json
{
"name": "testapp",
"version": "0.0.1",
"description": "Testapp",
"author": "hi there",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.16.4",
"mongodb": "^3.2.3"
}
}
dockerfile
FROM node:12-alpine
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
docker-compose.yml
version: '3'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- /usr/app/node_modules
ports:
- 80:8080
I have alread read this stackoverflow thread but I couldn't solve this issue. I'm getting this error no matter what module I'm trying to use: mongodb, spdy, etc. Locally without docker the testapp is working. But I don't know what I'm doing wrong with docker. Can anyone help me?
Have volume folder mapping for node_modules and ensure it has mongo folder copied / created
version: '3'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- ./src/node_modules:/usr/app/node_modules
ports:
- 80:8080
Ref:https://morioh.com/p/42531a398049/containerizing-a-node-js-application-for-development-with-docker-compose
You need to drop & rebuild the anonymous volume holding the node_modules for the updates to package.json to take effect.
docker-compose rm -v
# then:
docker-compose build
docker-compose up
You will only have to do this for changes to node_modules, since the other volume is linked to your ./src/, and will reflect changes on the host.
Related
I am getting this error in my code
mongodb-c | Error parsing command line: unrecognised option '--smallfiles'
mongodb-c | try 'mongod --help' for more information
mongodb-c exited with code 2
api_1 |
api_1 | > agte-api#1.0.0 start /app
api_1 | > nodemon index.js --watch
api_1 |
api_1 | [nodemon] 1.19.4
api_1 | [nodemon] to restart at any time, enter `rs`
api_1 | [nodemon] watching dir(s): *.*
api_1 | [nodemon] watching extensions: js,mjs,json
api_1 | [nodemon] starting `node index.js`
api_1 | (node:30) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
api_1 | Server is running on http://localhost:3000 or http://127.0.0.1:3000
api_1 | Mongoose connection error: MongoError: failed to connect to server [db:27017] on first connect [Error: getaddrinfo ENOTFOUND db
api_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26) {
api_1 | name: 'MongoError',
api_1 | message: 'getaddrinfo ENOTFOUND db'
api_1 | }]
api_1 | [nodemon] app crashed - waiting for file changes before starting...
Code:
version: '2'
services:
db:
image: mongo
container_name: 'mongodb-c'
ports:
- "27017:27017"
command:
- --smallfiles
# environment:
# - MONGO_INITDB_ROOT_USERNAME=superuser
# - MONGO_INITDB_ROOT_PASSWORD=admin
# - MONGO_INITDB_DATEBASE=admin
volumes:
- db:/data/db
api:
build: .
command: npm start
ports:
- 3000:3000
- 5858:5858
volumes:
- '/api'
depends_on:
- db
links:
- db
environment:
PORT: 3000
volumes:
db:
I'm having issues creating my first docker container. I keep getting an error saying Cannot find module '/src/bin/www' when I enter docker-compose up In the package.json file I tried "start": "node /bin/www" and "start": "node ./bin/www" but same issue. Also the first time I ran it, it took about a minute, but ever since that when I run it, it throws the error within seconds. Don't know if that's relevant.
> backend#1.0.0 start /src
node_1 | > node ./bin/www
node_1 |
node_1 | internal/modules/cjs/loader.js:983
node_1 | throw err;
node_1 | ^
node_1 |
node_1 | Error: Cannot find module '/src/bin/www'
node_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
node_1 | at Function.Module._load (internal/modules/cjs/loader.js:862:27)
node_1 | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
node_1 | at internal/main/run_main_module.js:17:47 {
node_1 | code: 'MODULE_NOT_FOUND',
node_1 | requireStack: []
node_1 | }
node_1 | npm ERR! code ELIFECYCLE
node_1 | npm ERR! errno 1
node_1 | npm ERR! backend#1.0.0 start: `node ./bin/www`
node_1 | npm ERR! Exit status 1
node_1 | npm ERR!
node_1 | npm ERR! Failed at the backend#1.0.0 start script.
node_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
node_1 |
node_1 | npm ERR! A complete log of this run can be found in:
node_1 | npm ERR! /root/.npm/_logs/2020-02-06T16_09_20_442Z-debug.log
myapp-docker_node_1 exited with code 1
Gracefully stopping... (press Ctrl+C again to force)
package.json
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node /bin/www"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.612.0",
"bcrypt": "^3.0.8",
"body-parser": "^1.19.0",
"create-hash": "^1.2.0",
"crypto": "^1.0.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.11",
"mongoose-unique-validator": "^2.0.3",
"multer": "^1.4.2",
"multer-s3": "^2.9.0"
}
}
docker-compose.yml
version: '3'
services:
web:
build: ./frontend
ports:
- "80:80"
links:
- node
volumes:
- "/Users/Phil/Documents/mysite/mysite-docker/frontend/dist:/usr/share/nginx/html"
node:
build: ./backend
ports:
- "3000:3000"
frontend docker file
FROM nginx
MAINTAINER Phil
VOLUME /Users/Phil/Documents/mysite/mysite-docker/frontend/dist:usr/share/nginx/html
EXPOSE 80
backend docker file
FROM node
MAINTAINER Phil
WORKDIR /src
COPY . /src
RUN npm install
RUN npm install -g nodemon
EXPOSE 3000
CMD ["npm", "start"]
In node_modules I don't find the "validator" package, but I don't understand why it is not installed with an npm with the dockerfile.
It's my project.
DockerFiles
FROM node:latest
WORKDIR /app
ADD restapi/* /app/
RUN npm install -g nodemon && \
npm install -g
RUN npm install -g validator
EXPOSE 3000
CMD ["nodemon"]
package.json
"dependencies": {
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.4",
"debug": "^4.1.1",
"express": "^4.16.4",
"firebase-admin": "^7.0.0",
"jade": "^1.11.0",
"kafka-node": "^4.0.2",
"mongoose": "^5.4.18",
"mongoose-float": "^1.0.3",
"morgan": "^1.9.1",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^8.1.0",
"validator": "^12.1.0"
}
docker-compose
version: "3"
services:
mongodb:
image: mongo:latest
container_name: mongo1
ports:
- 27017:27017
restapi:
build: .
image: restapi
container_name: restapi1
depends_on:
- mongodb
volumes:
- //c/Users/Halnap/restapi_project/restapi:/app
ports:
- 3000:3000
docker-compose return this error:
restapi1 | internal/modules/cjs/loader.js:957
restapi1 | throw err;
restapi1 | ^
restapi1 |
restapi1 | Error: Cannot find module 'validator'
restapi1 | Require stack:
restapi1 | - /app/models/users.js
restapi1 | - /app/database.js
restapi1 | - /app/index.js
restapi1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:954:17)
restapi1 | at Function.Module._load (internal/modules/cjs/loader.js:847:27)
restapi1 | at Module.require (internal/modules/cjs/loader.js:1016:19)
restapi1 | at require (internal/modules/cjs/helpers.js:69:18)
restapi1 | at Object.<anonymous> (/app/models/users.js:5:19)
restapi1 | at Module._compile (internal/modules/cjs/loader.js:1121:30)
restapi1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
restapi1 | at Module.load (internal/modules/cjs/loader.js:976:32)
restapi1 | at Function.Module._load (internal/modules/cjs/loader.js:884:14)
restapi1 | at Module.require (internal/modules/cjs/loader.js:1016:19)
restapi1 | at require (internal/modules/cjs/helpers.js:69:18)
restapi1 | at Object.<anonymous> (/app/database.js:45:1)
restapi1 | at Module._compile (internal/modules/cjs/loader.js:1121:30)
restapi1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
restapi1 | at Module.load (internal/modules/cjs/loader.js:976:32)
restapi1 | at Function.Module._load (internal/modules/cjs/loader.js:884:14) {
restapi1 | code: 'MODULE_NOT_FOUND',
restapi1 | requireStack: [ '/app/models/users.js', '/app/database.js', '/app/index.js' ]
restapi1 | }
restapi1 | [nodemon] app crashed - waiting for file changes before starting...
maybe I should add validator in package-lock.json?
in package-lock.json missing validator and bcryptjs (which I removed from dockerfiles), why is it not added with npm?
In execution it is added according to the screen of the powershell + validator#12.1.0
added 1 package from 2 contributors in 0.739s
In your DockerFiles, the validator package is being installed globally, rather than in the current directory. This is seen here in this command: npm install -g validator. You can see that nodemon is also being installed globally and doesn't appear in the package.json list of dependencies either.
If you want to install this package locally, run npm i -S validator.
Just install the validator package locally, run npm i -S validator i got the same issue and it is solved with installing it locally.
I am building out a ReactJS/Node.JS application in Docker. I am getting the following error where my package.json is never found:
server_1 |
server_1 | > stocklookback#1.0.0 dev /code/app/server
server_1 | > concurrently "npm run server" "npm run client"
server_1 |
server_1 | [1]
server_1 | [1] > stocklookback#1.0.0 client /code/app/server
server_1 | [1] > npm start
server_1 | [1]
server_1 | [0]
server_1 | [0] > stocklookback#1.0.0 server /code/app/server
server_1 | [0] > nodemon index.js
server_1 | [0]
client_1 | npm ERR! path /code/app/client/package.json
client_1 | npm ERR! code ENOENT
client_1 | npm ERR! errno -2
client_1 | npm ERR! syscall open
client_1 | npm ERR! enoent ENOENT: no such file or directory, open '/code/app/client/package.json'
client_1 | npm ERR! enoent This is related to npm not being able to find a file.
client_1 | npm ERR! enoent
client_1 |
client_1 | npm ERR! A complete log of this run can be found in:
client_1 | npm ERR! /root/.npm/_logs/2019-08-06T13_49_58_383Z-debug.log
server_1 | [1]
server_1 | [1] > stocklookback#1.0.0 start /code/app/server
server_1 | [1] > node index.js
server_1 | [1]
server_1 | [0] [nodemon] 1.19.1
server_1 | [0] [nodemon] to restart at any time, enter `rs`
server_1 | [0] [nodemon] watching: *.*
server_1 | [0] [nodemon] starting `node index.js`
stocklookback_client_1 exited with code 254
My Dockerfile for the client is:
FROM node:12.2.0-alpine
RUN mkdir -p /code/app/client
WORKDIR /code/app/client/
COPY package.json /code/app/client/
COPY package-lock.json /code/app/client/
RUN npm install
COPY . /code/app/client/
CMD [ "npm", "start" ]
The package.json is:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
And the docker-compose.yml is
version: '3.3'
volumes:
postgres_database :
external: false
services:
##########################
# Setup node container
##########################
server:
build: ./server
expose:
- ${APP_SERVER_PORT}
environment:
API_HOST: ${API_HOST}
APP_SERVER_PORT: ${APP_SERVER_PORT}
ports:
- ${APP_SERVER_PORT}:${APP_SERVER_PORT}
volumes:
- ./server:/code/app/server/src
command: npm run dev
##########################
# Setup client container
##########################
client:
build: ./client
environment:
- REACT_APP_PORT=${REACT_APP_PORT}
expose:
- ${REACT_APP_PORT}
ports:
- ${REACT_APP_PORT}:${REACT_APP_PORT}
volumes:
- ./client/src:/code/app/client/
- ./client/public:/code/app/client/public
links:
- server
command: npm start
The file structure is like so:
Why is my package.json not being copied over?
I have a node app that uses postgres and sequelize. I have a docker file that will run my server. I also have a docker compose file that will run a web image and db image that link my docker images and connect them together.
I am able to get my server running thorugh the docker file. I am trying to use the docker-compose file to run the database with it and and get them to work. I am getting a connection error with the database that is trying to connect and I am not sure where this error is coming from...
Dockerfile
FROM node:10
WORKDIR /app
COPY package.json ./app
RUN npm install
COPY . /app
CMD npm start
EXPOSE 5585
dock compose file:
version: "2"
services:
web:
build: .
ports:
- 80:5585
command: npm start
depends_on:
- db
environment:
- DATABASE_URL=postgres://username:password#db:5432/addidas
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=addidas
I am also using sequelize to connect my database with express. Do i need to change anythign to do that.
ERROR:
web_1 | Fri, 09 Nov 2018 18:26:47 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:242:13
web_1 | server is running at http://localhost:3001
web_1 | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1 | at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1 | at Connection.emit (events.js:182:13)
web_1 | at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1 | at Socket.emit (events.js:182:13)
web_1 | at emitErrorNT (internal/streams/destroy.js:82:8)
web_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
web_1 | name: 'SequelizeConnectionRefusedError',
web_1 | parent:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 },
web_1 | original:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 } }
web_1 | { SequelizeConnectionRefusedError: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at connection.connect.err (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:116:24)
web_1 | at Connection.connectingErrorHandler (/app/node_modules/pg/lib/client.js:140:14)
web_1 | at Connection.emit (events.js:182:13)
web_1 | at Socket.reportStreamError (/app/node_modules/pg/lib/connection.js:71:10)
web_1 | at Socket.emit (events.js:182:13)
web_1 | at emitErrorNT (internal/streams/destroy.js:82:8)
web_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
web_1 | at process._tickCallback (internal/process/next_tick.js:63:19)
web_1 | name: 'SequelizeConnectionRefusedError',
web_1 | parent:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 },
web_1 | original:
web_1 | { Error: connect ECONNREFUSED 172.19.0.3:5432
web_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
web_1 | errno: 'ECONNREFUSED',
web_1 | code: 'ECONNREFUSED',
web_1 | syscall: 'connect',
web_1 | address: '172.19.0.3',
web_1 | port: 5432 } }
1st change to 5432 to defalut postgres port
db:
image: postgres
restart: always
ports:
- "5432:3306" //default postgres port
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
- POSTGRES_DB=addidas
Allow the port you want to connect
sudo ufw allow 5432
try:
...
web:
build:
context: .
ports:
- 80:5585
command: npm start
network_mode: service:db
links:
- db
environment:
- DATABASE_URL=postgres://username:password#db:5432/addidas
...
Another thing is that from the expose line I guess that you want to run the node app on port 5585. But it is actually running on port 3001 (Based on the 2. line of the error message)