你不应该用
ng serve
在生产Docker容器中,因为它调用开发HTTP服务器,根本不安全,只是为了在本地测试应用程序。这就是为什么命令
ng serve --prod
触发以下警告:
****************************************************************************************
This is a simple server for use in testing or debugging Angular applications locally. It hasn't been reviewed for security issues.
DON'T USE IT FOR PRODUCTION!
****************************************************************************************
我将给您一个Dockerfile用Nginx HTTP服务器(两个最广泛使用的服务器之一)包装一个角度应用程序的示例:
FROM node:10.13.0-stretch as build-stage
WORKDIR /app
COPY package*.json /app/
RUN npm install
RUN npm install -g --unsafe-perm @angular/cli@latest
COPY ./ /app/
ARG configuration=production
RUN ng build --prod --build-optimizer=true --aot=true --output-hashing=all --extract-css=true --named-chunks=false --vendor-chunk=true
FROM nginx:1.14.1
# Add application files
COPY --from=build-stage /app/dist/name-of-your-app/ /var/www/html
COPY --from=build-stage /app/nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=build-stage /app/nginx/site.conf /etc/nginx/conf.d/default.conf
RUN touch /var/run/nginx.pid && \
chown -R www-data: /etc/nginx/ && \
chown -R www-data: /var/run/nginx.pid && \
chown -R www-data: /var/cache/nginx && \
chown -R www-data: /var/www/html
USER www-data
#Expose the port
EXPOSE 8080
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]
这假设你有适当的
nginx.conf
site.conf
在一个
nginx
保存证书配置的文件夹。