0

I have a NestJS application that lives in a monorepo. My webpack config has implicit dependencies to generate a package.json, so my dist/apps/server folder contains

- package.json
- package-lock.json
- main.js
- main.js.map

My Dockerfile contains the following:

FROM node:12.22.1-alpine3.10
RUN ls
COPY ../../dist/apps/server .
RUN npm install --production
ENV PORT 3333
EXPOSE 3333
CMD node main.js

I'm able to pull my /dist output into a new directory, npm install, and run the project fine, but for some reason when I run in a docker container, I never get the logs in `app.listen()

  await app.listen(port, '0.0.0.0', () => {
    Logger.log('Listening at http://localhost:' + port + '/' + globalPrefix);
  }).catch(err => console.log(err));

Whenever I hit localhost:3333 I get ERR_EMPTY_RESPONSE, and after a few minutes my container gives me a fatal out of memory error

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

But on startup, I get all of NestJS logging for the RouteExplorer, RoutesResolver, InstanceLoader, etc... It's only missing the app.listen() call.

My docker run command is

docker run -p 3333:3333 server where 'server' is the appropriate tag. Why is my application failing to listen for requests inside a Docker Container but successful when I run locally?

JamesRichardson
  • 131
  • 2
  • 13
  • Add `GENERATE_SOURCEMAP=false` to your `Dockerfile` right before the build command. – Ashok Apr 24 '21 at 06:06
  • You have a memory leak somewhere in your project or the application is very big and node cannot allocate enough memory. See https://stackoverflow.com/questions/53230823/fatal-error-ineffective-mark-compacts-near-heap-limit-allocation-failed-javas – PVermeer Apr 24 '21 at 10:11
  • Could also be the a difference in node versions (local - docker). Als try the full node docker image (non alpine). The alpine image is a very slimmed down container that might require you to manage some dependencies manually depending on your app. – PVermeer Apr 24 '21 at 10:16
  • @PVermeer I've tried increasing the heap size and no longer get an out of memory error, I've also updated to use `FROM node:14`, but the new error I get is `[Error: ENOTDIR: not a directory, scandir '/proc/1/task/1/cwd/dev/fd/22'] `, any ideas? Not sure what is attempting to access this directory or why. – JamesRichardson Apr 25 '21 at 01:39
  • @JamesRichardson I think the issue is also that you do not build your project in the container. If you build on one system and then try to use on another, paths / versions / pre/post install scripts / etc. might differ. So I would advise to copy to whole project source in the docker file, then npm ci (ci ensures same versions from dev), then build, then run. You can use the docker multi-stage builds to keep the final image as lean as possible. – PVermeer Apr 25 '21 at 09:46

0 Answers0