'Run Angular project in Docker without node_modules

I'm trying to run my angular app in Docker. The problem is, when I remove node_modules folder from project there is an error: An unhandled exception occurred: Cannot find module '@angular-devkit/build-angular/package.json'. I can run project locally to create node_module and then running in docker is successfully.

My Dockerfile-angular file:

FROM node:12.14.0

WORKDIR /app

COPY ./angular_app/package.json /app/package.json

RUN npm install -g @angular/[email protected]
RUN npm install

ENTRYPOINT ["ng","serve","--host","0.0.0.0","--disableHostCheck"]

My package.json :

{
  "name": "fundacja",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --host 0.0.0.0",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.1.9",
    "@angular/common": "~9.1.9",
    "@angular/compiler": "~9.1.9",
    "@angular/core": "~9.1.9",
    "@angular/forms": "~9.1.9",
    "@angular/platform-browser": "~9.1.9",
    "@angular/platform-browser-dynamic": "~9.1.9",
    "@angular/router": "~9.1.9",
    "@angular/youtube-player": "^11.0.3",
    "angular-countdown-timer": "0.0.1",
    "angular-oauth2-oidc": "^10.0.3",
    "bootstrap-cookie-alert": "^1.2.1",
    "hammerjs": "^2.0.8",
    "jwt-decode": "^3.1.2",
    "ng2-cookies": "^1.0.12",
    "ng2-date-countdown": "^1.1.3",
    "ng2-file-upload": "^1.4.0",
    "ngx-cookie-service": "^11.0.2",
    "ngx-countdown": "^11.0.1",
    "ngx-gallery-9": "^1.0.6",
    "ngx-image-gallery": "^2.0.5",
    "rxjs": "~6.5.4",
    "rxjs-compat": "^6.6.2",
    "screenfull": "^3.3.0",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.901.13",
    "@angular/cli": "~9.1.7",
    "@angular/compiler-cli": "~9.1.9",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  }
}

Exception in terminal looks like that:

frontend      | An unhandled exception occurred: Cannot find module '@angular-devkit/build-angular/package.json'
frontend      | Require stack:
frontend      | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/node-modules-architect-host.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/index.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/models/architect-command.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/commands/serve-impl.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/export-ref.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/index.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/utilities/json-schema.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/models/command-runner.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/lib/cli/index.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/lib/init.js
frontend      | - /usr/local/lib/node_modules/@angular/cli/bin/ng
frontend      | See "/tmp/ng-cbsfRE/angular-errors.log" for further details.

I have no Idea why locally my project is compiled successfully without node_modules, but in Docker it didn't.

My ng v :

Angular CLI: 13.3.0
Node: 16.14.1
Package Manager: npm 8.5.0
OS: linux x64

Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1303.0 (cli-only)
@angular-devkit/core         13.3.0 (cli-only)
@angular-devkit/schematics   13.3.0 (cli-only)
@schematics/angular          13.3.0 (cli-only)

Thanks for any help.



Solution 1:[1]

In Docker documentation there's what's called .dockerignore, where you can find what you are looking for to get out node modules. You make your installation normally in the container then you tell docker to ignore certain modules like node modules.

https://docs.docker.com/engine/reference/builder/#dockerignore-file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman