Express server can't read key in Docker container - docker

I am dockerizing a system that uses a pem and key to authenticate with a remote dev server. Unfortunately, it would appear that it is unable to find the file despite it being in the docker image.
Error
server_1 | Error: ENOENT: no such file or directory, open '/app/certificates/jwt.key'
server_1 | at Object.openSync (fs.js:497:3)
server_1 | at Object.readFileSync (fs.js:393:35)
server_1 | at Object.<anonymous> (/app/controllers/user.js:7:16)
server_1 | at Module._compile (internal/modules/cjs/loader.js:1085:14)
server_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
server_1 | at Module.load (internal/modules/cjs/loader.js:950:32)
server_1 | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
server_1 | at Module.require (internal/modules/cjs/loader.js:974:19)
server_1 | at require (internal/modules/cjs/helpers.js:101:18)
server_1 | at Object.<anonymous> (/app/routes/upload.js:8:24)
server_1 | at Module._compile (internal/modules/cjs/loader.js:1085:14)
server_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
server_1 | at Module.load (internal/modules/cjs/loader.js:950:32)
server_1 | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
server_1 | at Module.require (internal/modules/cjs/loader.js:974:19)
server_1 | at require (internal/modules/cjs/helpers.js:101:18) {
server_1 | errno: -2,
server_1 | syscall: 'open',
server_1 | code: 'ENOENT',
server_1 | path: '/app/certificates/jwt.key'
server_1 | }
Line (server/controllers/user.js -> app/controllers/user.js)
const pem = fs.readFileSync(path.resolve(__dirname, "../certificates/jwt.key"));
Dockerfile
FROM node:14
RUN mkdir app
WORKDIR /app
COPY . .
RUN npm install
RUN npm run get_key
EXPOSE 5000
CMD [ "npm", "run", "start" ]
package.json
"scripts": {
"test": "mocha -r dotenv/config ./**/*.test.js",
"start": "node -r dotenv/config server.js",
"dev": "nodemon -r dotenv/config server.js",
"get_key": "ssh-keygen -t rsa -b 4096 -m PEM -f ./certificates/jwt.key -P ''"
},

The volume mount was overriding the key building. Need to generate the key locally first before composing.
server:
build: server
ports:
- "5000:5000"
restart: always
depends_on:
- mongodb
volumes:
- ./server:/app

Related

Docker "Unexpected identifier" error for library express

I study containers. For it, I implement it in a particular project, available on this link. When I type the command docker-compose up, an error log below pops out. I can run the command npm start without problems.
Attaching to nodejs
nodejs | [nodemon] 2.0.15
nodejs | [nodemon] to restart at any time, enter `rs`
nodejs | [nodemon] watching path(s): *.*
nodejs | [nodemon] watching extensions: js,mjs,json
nodejs | [nodemon] starting `node app.js ./src/index.js`
nodejs | /home/node/app/src/index.js:2
nodejs | import express from 'express';
nodejs | ^^^^^^^
nodejs |
nodejs | SyntaxError: Unexpected identifier
nodejs | at Module._compile (internal/modules/cjs/loader.js:723:23)
nodejs | at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
nodejs | at Module.load (internal/modules/cjs/loader.js:653:32)
nodejs | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
nodejs | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
nodejs | at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
nodejs | at startup (internal/bootstrap/node.js:283:19)
nodejs | at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
nodejs | [nodemon] app crashed - waiting for file changes before starting...

Nest JS doesn't have permissions to copy GRPC files inside docker

I have a docker container running via docker-compose with the command as running nestjs in dev mode:
FROM node:16-alpine as base
RUN apk add --no-cache libc6-compat tini
FROM base as dev
ENV NODE_ENV development
USER node
WORKDIR /home/node
# copy all files over
COPY --chown=node:node ./ ./
RUN mkdir -p ./my-app/dist/shared/grpc
RUN chown -R node:node ./my-app/dist
RUN chown -R node:node ./my-app/dist/shared/grpc
My grpc files are in a shared project. the full structure is:
services/shared
services/my-app
The shared project has grpc files inside the directory shared/grpc.
NestJS copies these over to it's dist folder when building. As this is dev, that's every code change or on docker error:
my-app/nest-cli.json:
{
"collection": "#nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [
{
"include": "../../shared/grpc/*.proto",
"outDir": "./dist/shared/grpc"
}
],
"watchAssets": true
},
"entryFile": "/my-app/src/main"
}
NestJS seems to detect 0 errors, but fails on the copying of the grpc files:
my-app_1 | [7:06:11 AM] Found 0 errors. Watching for file changes.
my-app_1 |
my-app_1 | node:fs:1828
my-app_1 | handleErrorFromBinding(ctx);
my-app_1 | ^
my-app_1 |
my-app_1 | Error [ShellJSInternalError]: EPERM: operation not permitted, chmod 'dist/shared/grpc/apps.proto'
my-app_1 | at Object.chmodSync (node:fs:1828:3)
my-app_1 | at copyFileSync (/home/node/my-app/node_modules/shelljs/src/cp.js:78:8)
my-app_1 | at /home/node/my-app/node_modules/shelljs/src/cp.js:298:7
my-app_1 | at Array.forEach (<anonymous>)
my-app_1 | at Object._cp (/home/node/my-app/node_modules/shelljs/src/cp.js:243:11)
my-app_1 | at Object.cp (/home/node/my-app/node_modules/shelljs/src/common.js:384:25)
my-app_1 | at AssetsManager.actionOnFile (/home/node/my-app/node_modules/#nestjs/cli/lib/compiler/assets-manager.js:95:19)
my-app_1 | at FSWatcher.<anonymous> (/home/node/my-app/node_modules/#nestjs/cli/lib/compiler/assets-manager.js:70:47)
my-app_1 | at FSWatcher.emit (node:events:520:28)
my-app_1 | at FSWatcher.emitWithAll (/home/node/my-app/node_modules/chokidar/index.js:540:8) {
my-app_1 | errno: -1,
my-app_1 | syscall: 'chmod',
my-app_1 | code: 'EPERM',
my-app_1 | path: 'dist/shared/grpc/apps.proto'
my-app_1 | }
my-app_1 | error Command failed with exit code 1.
my-app_1 | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
my-app_1 | yarn run v1.22.17
However I can't work out why - it has ownership of all the folders it needs to access. Here's my docker-compose:
version: '3.8'
services:
my-app:
build:
context: .
target: dev
dockerfile: ./my-app/Dockerfile
restart: always
user: node
ports:
- 3003:3000
volumes:
- ./my-app:/home/node/my-app
- ./shared:/home/node/shared
working_dir: /home/node/my-app
command: yarn run start:dev
volumes:
my-app:
Edit 1
After removing USER node from Dockerfile and docker-compose this works. But that means node is running as root which is not OK and not a secure solution. I have even tried adding RUN chmod -R 777 /home/node within Dockerfile and that doesnt work. There must be something behind the scenes in nestjs that needs specific permissions but I can't figure out what.
Bit late to the party, but it was my nest-cli.json file that was causing the issue for me.
under compilerOptions I listed assets that aren't normally watched by Nest (ie: .proto or in my case .ejs files). Removing that line fixed it for me, so it had to do with me running the app in --watch mode and it needing to hot reload that file and not having permissions to copy it.
nest-cli.json:
{
"collection": "#nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [
"app/**/**/*.ejs"
],
"watchAssets": true
}
}

docker-compose up command throwing error when deploying a parse server

I am trying to run my parse server app in docker, but when running the command 'docker-compose up' it runs fine for some seconds and then throws this error:
Error parsing command line: unrecognised option '--break-mongo'.
and after this it also throws an npm related error --
my-parse-app |
my-parse-app | > app#1.0.0 start /parse-server
my-parse-app | > parse-server "/parse-server/config/config.json"
my-parse-app |
my-parse-app |
/parse-server/node_modules/parse/lib/node/ParseObject.js:1301
my-parse-app | async exists(options
my-parse-app | ^^^^^^
my-parse-app |
my-parse-app | SyntaxError: Unexpected identifier
my-parse-app | at createScript (vm.js:56:10)
my-parse-app | at Object.runInThisContext (vm.js:97:10)
my-parse-app | at Module._compile (module.js:549:28)
my-parse-app | at Object.Module._extensions..js (module.js:586:10)
my-parse-app | at Module.load (module.js:494:32)
my-parse-app | at tryModuleLoad (module.js:453:12)
my-parse-app | at Function.Module._load (module.js:445:3)
my-parse-app | at Module.require (module.js:504:17)
my-parse-app | at require (internal/module.js:20:19)
my-parse-app | at Object.
(/parse-server/node_modules/parse/lib/node/ParseRole.js:12:43)
my-parse-app |
my-parse-app | npm ERR! Linux 4.19.128-microsoft-standard
my-parse-app | npm ERR! argv "/usr/local/bin/node"
"/usr/local/bin/npm" "start" "--" "/parse-server/config/config.json"
my-parse-app | npm ERR! node v6.17.1
my-parse-app | npm ERR! npm v3.10.10
my-parse-app | npm ERR! code ELIFECYCLE
my-parse-app | npm ERR! app#1.0.0 start: parse-server "/parse-server/config/config.json"
my-parse-app | npm ERR! Exit status 1
my-parse-app | npm ERR!
my-parse-app | npm ERR! Failed at the app#1.0.0 start script
'parse-server "/parse-server/config/config.json"'.
my-parse-app | npm ERR! Make sure you have the latest version of
node.js and npm installed.
my-parse-app | npm ERR! If you do, this is most likely a problem with
the app package,
my-parse-app | npm ERR! not with npm itself.
my-parse-app | npm ERR! Tell the author that this fails on your
system:
my-parse-app | npm ERR! parse-server
"/parse-server/config/config.json"
my-parse-app | npm ERR! You can get information on how to open an
issue for this project with:
my-parse-app | npm ERR! npm bugs app
my-parse-app | npm ERR! Or if that isn't available, you can get their
info via:
my-parse-app | npm ERR! npm owner ls app
my-parse-app | npm ERR! There is likely additional logging output
above.
my-parse-app |
my-parse-app | npm ERR! Please include the following file with any
support request:
my-parse-app | npm ERR! /parse-server/npm-debug.log
my-parse-app exited with code 1
My docker-compose:
version: "2"
services:
# Node.js parse-server application image
app:
build: ./app
command: npm start -- /parse-server/config/config.json
container_name: my-parse-app
volumes:
- ./app:/parse-server/
- /parse-server/node_modules
ports:
- "1337:1337"
links:
- mongo
# MongoDB image
mongo:
image: mongo:latest
container_name: mongo-database
ports:
- "27017:27017"
volumes_from:
- mongodata
# MongoDB image volume for persistence
mongodata:
image: mongo
volumes:
- ./data/db:/data/db
command:
- --break-mongo
My Dockerfile
FROM node:boron
RUN mkdir -p /parse-server
WORKDIR /parse-server
COPY . /parse-server
RUN npm install
EXPOSE 1337
CMD ["npm","start"]
The break-mongo error
The npm error
It looks you are using an old version of node and it is probably the problem. Switch the first line of your docker file to:
FROM node:erbium

Rails webpacker for some reason always think NODE_ENV is production despite I use `webpack-dev-server` and set NODE_ENV to development

As title, I am using docker ruby 2.3.5 (ubuntu) environment, despite I have already setup NODE_ENV=development, when I try to intercept the value of process.env.NODE_ENV I still see production, which I can't find anywhere it has been overridden. It only happens in the docker environment but not on my MacOS, not sure whats the reason?
Update: Here is Dockerfile and docker-compose.yml for reference:
https://gist.github.com/goodwill/b4e677ccf8fe0079183adeec35218812
Update: This is the error I end up with when running webpacker-dev-server:
04:08:37 webpacker.1 | /app/config/webpack/development.js:20
04:08:37 webpacker.1 | https: settings.dev_server.https,
04:08:37 webpacker.1 | ^
04:08:37 webpacker.1 |
04:08:37 webpacker.1 | TypeError: Cannot read property 'https' of undefined
04:08:37 webpacker.1 | at Object.<anonymous> (/app/config/webpack/development.js:20:31)
04:08:37 webpacker.1 | at Module._compile (module.js:570:32)
04:08:37 webpacker.1 | at Object.Module._extensions..js (module.js:579:10)
04:08:37 webpacker.1 | at Module.load (module.js:487:32)
04:08:37 webpacker.1 | at tryModuleLoad (module.js:446:12)
04:08:37 webpacker.1 | at Function.Module._load (module.js:438:3)
04:08:37 webpacker.1 | at Module.require (module.js:497:17)
04:08:37 webpacker.1 | at require (internal/module.js:20:19)
04:08:37 webpacker.1 | at requireConfig (/app/node_modules/webpack/bin/convert-argv.js:97:18)
04:08:37 webpacker.1 | at /app/node_modules/webpack/bin/convert-argv.js:104:17
Docker containers will not collect environment variables from your local environment.
Set the environmental variable inside your Dockerfile. For example:
ENV NODE_ENV=development
Alternatively, you can run the container with that environmental variable from the command line:
$ docker run -e NODE_ENV=devlopment mycontainer

npm broken inside Docker container

So, I updated npm inside docker container and since I have node version 6.9.4, npm is broken. Any npm commands will throw this error.
client | module.js:471
client | throw err;
client | ^
client |
client | Error: Cannot find module 'process-nextick-args'
client | at Function.Module._resolveFilename (module.js:469:15)
client | at Function.Module._load (module.js:417:25)
client | at Module.require (module.js:497:17)
client | at require (internal/module.js:20:19)
client | at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
client | at Module._compile (module.js:570:32)
client | at Object.Module._extensions..js (module.js:579:10)
client | at Module.load (module.js:487:32)
client | at tryModuleLoad (module.js:446:12)
client | at Function.Module._load (module.js:438:3)
and I would like to downgrade npm to 3.10.10.
Is there any way to do this without recreating the container?
I'm using docker-compose.

Resources