Hi,
The YAML file is as follows:
services:
nginx:
container_name: Nginx
build:
context: .
dockerfile: ./cfiles/Dockerfile-2
ports:
- '80:80'
volumes:
- ./cfiles/default.conf:/etc/nginx/conf.d/default.conf
- ./.:/usr/share/nginx/html
depends_on:
- nodejs
links:
- nodejs
nodejs:
container_name: NodeJS
build:
context: .
dockerfile: ./cfiles/Dockerfile-1
command: npm start
volumes:
- ./.:/usr/src/app
ports:
- 3000
I created two Dockerfile
s as follows:
Dockerfile-1:
FROM nginx:latest
COPY . /usr/share/nginx/html
COPY ./cfiles/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD nginx -g 'daemon off;'
Dockerfile-2:
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./package.json /usr/src/app/package.json
RUN npm install
RUN npm update
COPY . /usr/src/app
EXPOSE 3000
The package.json
file is as follows:
{
"name": "demo",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"test": "mocha --exit"
},
"dependencies": {
"cookie-parser": "^1.4.5",
"debug": "^4.3.1",
"express": "^4.17.1",
"http-errors": "^1.8.0",
"morgan": "^1.10.0",
"pug": "^3.0.2"
},
"devDependencies": {
"mocha": "^9.0.0",
"supertest": "^6.1.3"
}
}
The app.js
is :
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
The Node.js
container does not run and shows the following error:
> demo@0.0.0 start
> node ./bin/www
node:internal/modules/cjs/loader:1145
throw err;
^
Error: Cannot find module 'http-errors'
Require stack:
- /usr/src/app/app.js
- /usr/src/app/bin/www
at Module._resolveFilename (node:internal/modules/cjs/loader:1142:15)
at Module._load (node:internal/modules/cjs/loader:983:27)
at Module.require (node:internal/modules/cjs/loader:1230:19)
at require (node:internal/modules/helpers:179:18)
at Object.<anonymous> (/usr/src/app/app.js:1:19)
at Module._compile (node:internal/modules/cjs/loader:1368:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
at Module.load (node:internal/modules/cjs/loader:1205:32)
at Module._load (node:internal/modules/cjs/loader:1021:12)
at Module.require (node:internal/modules/cjs/loader:1230:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/usr/src/app/app.js', '/usr/src/app/bin/www' ]
}
Node.js v21.7.3
I added the following line to the Dockerfile
:
RUN npm ls http-errors
The result is:
#11 [nodejs 7/8] RUN npm ls http-errors
#11 1.440 demo@0.0.0 /usr/src/app
#11 1.440 +-- express@4.19.2
#11 1.440 | +-- body-parser@1.20.2
#11 1.440 | | +-- http-errors@2.0.0
#11 1.440 | | `-- raw-body@2.5.2
#11 1.440 | | `-- http-errors@2.0.0
#11 1.440 | +-- http-errors@2.0.0
#11 1.440 | `-- send@0.18.0
#11 1.440 | `-- http-errors@2.0.0
#11 1.440 `-- http-errors@1.8.1
What is wrong?
Cheers.