4

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",
...

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.