11

I have an angular app which I dockerized together with nginx. My dockerfile:

FROM node:8.11.3 as node

WORKDIR /app

COPY package.json /app/

RUN npm install
COPY ./ /app/

ARG env=prod

RUN npm run build -- --prod --environment $env

FROM nginx:1.13

COPY --from=node /app/dist/ /usr/share/nginx/html

RUN rm /etc/nginx/conf.d/default.conf
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf

EXPOSE 4200
CMD ["npm", "start"]

I build my docker image but when I start docker run I get following error message:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"npm\": executable file not found in $PATH": unknown.

If I remove the last command from my Dockerfile (CMD ["npm","start"] I can run my docker image - it works. But I guess I need the last command so I can deploy and run my app in AWS. Also 'CMD npm start' doesnt work.

I have seen a couple of post about similar issues but non of the solutions worked for me.

UPDATE:

I removed the last line - CMD npm start. ECS is now trying to start my task - but it stops with exit code 1 and no further comments. Any idea?

Thanks, Michael

Michael H.
  • 471
  • 2
  • 9
  • 22
  • When running an Angular app, you shouldn't use `npm start`. That command is for creating a dev server to use while developing. Take a look at https://angular.io/guide/deployment. Also, if you want to run it in AWS, it would be much cheaper to serve it as static content instead of as a Docker image – user184994 Jul 14 '18 at 10:04
  • What is the right way to start it? If I use CMD["ng", "serve", "-H", "0.0.0.0"] I get the same error message. – Michael H. Jul 14 '18 at 10:06
  • Hi, you mean I shouldnt use docker at all for AWS? – Michael H. Jul 14 '18 at 10:08
  • As far as understood, you second `FROM` instruction overrides first one, so at the moment when `CMD` is run, no `npm` executable available. Please check this: https://stackoverflow.com/questions/33322103/multiple-froms-what-it-means – Sergii Vorobei Jul 14 '18 at 10:09
  • Not at all, there are times when Docker in AWS is great, but using Docker to serve Angular is really over the top. Once built, Angular is just a series of static files, and it would be much cheaper to use cloudfront or S3 static hosting – user184994 Jul 14 '18 at 10:09
  • ok - need to optimize this lateron but dont know cloudront or s3. But is there a way I can get it working with docker? If I remove the CMD instruction it works perfectly fine - I can start with docker run and my angular server is up and running. The problem is I dont get it up and running in AWS - I thought the issue is the missing CMD instruction. But perhaps I am wrong here? – Michael H. Jul 14 '18 at 10:19
  • I think you'll spend far more time trying to configure docker / nginx than if you deployed to S3. It's literally a drag and drop. Build your Angular project on your machine first using `ng build`, then copy the contents of the `dist` directory to S3. [Here is a guide if it helps](https://medium.com/@lockdown2k17/aws-s3-static-website-hosting-using-ssl-acm-112e6d6b6d97#1f89) – user184994 Jul 14 '18 at 10:28
  • Ok - will take a look at it. I only thought I should be able to deploy it dockerised very quickly in AWS since I already made it running on my local machine. I need nginx since my Angular front end will communicates with a rails server and I need a proxy server to avoid CORS – Michael H. Jul 14 '18 at 10:30
  • But if you're using nginx to serve it, there is literally no need to use `npm start`, nor is there any need to access port 4200, seeing as nginx is probably listeing on 80, and port 4200 represents the dev server – user184994 Jul 14 '18 at 10:32
  • Ok - so I can remove this from my dockerfile (and this way it also works on my local machine). I thought this was the issue I cannot connect to my Angular app through AWS. When I use the image without the non start and go to the public IP:4200 I don’t see my website. I checked almost everything - all all seems to look fine on AWS. Still my website doesn’t come up and the hype requests times out. So looks like nginx is not getting the request – Michael H. Jul 14 '18 at 10:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174990/discussion-between-user184994-and-michael-h). – user184994 Jul 14 '18 at 10:37

1 Answers1

4

The nginx docker image doesn't contain npm. You'll need to use node:latest or node:alpine which contains npm.

Tristan Müller
  • 365
  • 2
  • 14
  • Docker file contains multiple FROM clauses (one is node, other is nginx) - that part is probably not handled correctly. – Petr Gladkikh Mar 04 '20 at 13:38
  • why would he want to change to an image that has no nginx? that makes no sense, this answer makes no sense. He's asking how can he get npm and yarn working on the nginx container. If it's not there then maybe he needs to install both npm and yarn – PositiveGuy Sep 12 '20 at 01:19