hY8vVpf3tyR57Xib
hY8vVpf3tyR57Xib

Reputation: 3915

Dockerfile for angular-cli application returns ng: command not found

I am trying to run my Angular application (generated with the ng-cli) in a docker file.

This is my docker file:

FROM node:6.10.0

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN npm install --dev && npm cache clean

COPY . /usr/src/app

CMD [ "ng", "serve" ]

This however crashes with the command:

ng: command not found

How is this possible? Angular CLI is listed in my development depencies of my package.json.

  "devDependencies": {
    "@angular/cli": "1.4.4",
    "@angular/compiler-cli": "^4.2.4",
    "@angular/language-service": "^4.2.4",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.2.0",
...

Upvotes: 4

Views: 7419

Answers (1)

Menzo Wijmenga
Menzo Wijmenga

Reputation: 1121

The problem is that the command ng is not found in your $PATH variable. When you run npm install --dev it will install your dependencies within the scope of your project.

Commands invoked through the scripts property in you package.json will be able to find your local dependencies.

Example package.json:

{
  "name": "example-package",
  "version": "1.0.0",
  "description": "Example package that starts ng",
  "scripts": {
    "start": "ng serve"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    ...
  }
}

Dockerfile:

FROM node:6.10.0

RUN mkdir -p /usr/src/app WORKDIR /usr/src/app

COPY package.json /usr/src/app/ RUN npm install --dev && npm cache clean

COPY . /usr/src/app

CMD [ "npm", "start" ]

Note that npm supports only a handful of scripts out of the box. If you want run arbitrary scripts you use the npm run-script ... command.

...
"scripts" : {
   "ng-serve": "ng serve"
}
...

Run the script by calling: npm run-script ng-serve

You can find a complete list of default scripts and more information in the npm docs here: https://docs.npmjs.com/misc/scripts

Upvotes: 3

Related Questions