title: How to write Dockerfile to serve Angular app and Node server
body: <p>My Angular app runs fine locally but I haven't figured out how to do the same with a Docker image. Outside of Docker, the UI runs on port 4200 with <code>ng serve</code> and the API serves data from 8080 with <code>node server.js</code>.</p>
  <p>My Dockerfile is set up so it can get the Node server running and available on 8080, but the Angular UI won't run. I've tried several options but right now I have:</p>
  <pre><code>FROM node:14.17.3
  COPY package*.json ./
  EXPOSE 4200 8080
  RUN npm install -g @angular/cli
  RUN npm install --only=production
  COPY . ./
  RUN ng serve
  CMD [&quot;node&quot;, &quot;server.js&quot;]
  </code></pre>
  <p>It fails on <code>ng serve</code> with the error: <code>The serve command requires to be run in an Angular project, but a project definition could not be found</code>. I do have an angular.json file in the root. I'm not sure what I am missing. I read that <code>ng serve</code> shouldn't be used in this situation but the alternatives I've seen haven't made a difference.</p>
  <p>Workspace:
  <a href="https://i.stack.imgur.com/35VYz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/35VYz.jpg" alt="enter image description here" /></a></p>
  <p>EDIT 8/10/21: Based on the answers here and a bunch of research, this will display the UI with nginx:</p>
  <pre><code>FROM node:12.16.1-alpine as build
  WORKDIR /usr/src/app
  COPY package*.json ./
  RUN npm ci --only=production
  COPY . .
  # RUN npm install -g @angular/cli
  # RUN npm run build --prod
  FROM nginx:1.15.8-alpine
  COPY --from=build /usr/src/app/dist /usr/share/nginx/html
  # CMD [&quot;node&quot;, &quot;server.js&quot;]
  </code></pre>
  <p>However, the npm run build step fails because ng is not found despite installing @angular/cli. I have to run this manually to build the dist folder. And I can't run <code>node server.js</code> alongside this. It seems I can only get the front end or back end, not both.</p>