Method steps for deploying an Angular project using Docker
There are two ways for Docker to deploy an Angular project, one is server-side rendering, which is described in the official document, and the other is to use a node image to compile and put it into the web server. Because of the node environment, it is most convenient to use express.
Create server.js
Const express = require ('express'); const app = express (); const config = {root: _ dirname +' / dist', port: process.env.PORT | | 4200}; / / static resource app.use ('/', express.static (config.root)); / / all routes go to index.htmlapp.all ('*', function (req, res) {res.sendfile (config.root +'/ index.html');}) App.listen (config.port, () = > {console.log ("running.") ;})
Create Dockerfile
FROM node:13.3.0-alpine3.10ENV PORT=4200\ NODE_ENV=production# install express and angular/cliRUN npm install express@4.17.1-g\ & & npm install-g @ angular/cli# create the app directory RUN mkdir-p / app# copy the code to the App directory COPY. / appWORKDIR / app# installation depends on building the program. Here, since I need to reverse proxy to the subdirectory, I added the base-href parameter RUN npm install & & ng build-- base-href / manage/-- prodEXPOSE ${PORT} ENTRYPOINT ["node", "/ app/server.js"]
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.