如何在Linux上使用Docker和Azure Web App部署Angular 4应用程序

我正试图按照这个文档部署我的angular4 Web应用程序与docker。

Linux上的Azure Web App文档

以下是我的Docker文件目前的样子:

# Create image based on the official Node 6 image from dockerhub FROM node:6 # Create a directory where our app will be placed RUN mkdir -p /usr/src/app # Change directory so that our commands run inside this new directory WORKDIR /usr/src/app # Copy dependency definitions COPY package.json /usr/src/app # Install dependecies RUN npm install # Get all the code needed to run the app COPY . /usr/src/app # Expose the port the app runs in EXPOSE 80 # Serve the app CMD ["npm", "start"] 

我的package.json currenly下有脚本"start": "ng serve -H 0.0.0.0",

我能够在Linux上创buildWeb应用程序并推送我的图像,但是当我尝试浏览Web应用程序时,我收到了以下内容:

无效的主机头

我在我的docker文件中丢失了什么? 我有点卡住在这一点,所以任何帮助,将不胜感激。

如果您只是提供生成的应用程序,尽可能简单。 你真的需要容器中的ng-cli和图像中的所有节点模块吗? 单独的节点模块至less为200 MB。

使用ng-build --prod在本地构build应用程序。 这会生成dist文件夹,这是您唯一需要的。

你最终得到一个非常简单的Dockerfile

 FROM nginx:stable-alpine ADD dist /usr/share/nginx/html ADD default.conf /etc/nginx/conf.d/ 

基本的nginx conf来处理angular度为default.conf URL:

 server { listen 80; server_name localhost; root /usr/share/nginx/html; charset utf-8; location / { try_files $uri /index.html; } } 

这给你一个基于你的应用程序的大小,而不是250 MB +的所有工具和依赖关系,可能30 MB的小图像。

我认为内置的networking服务器需要一个不同的主机。 您可以尝试使用“ng serve -host 0.0.0.0”而不是npm-start命令。 对于生产用途,我build议采取不同的方法,但是:我会使用nginx(或任何其他)生产Web服务器来服务angular应用程序,并build立一个图像,只包含最终的应用程序交付。 如果你想使用docker作为构build容器,我将分开这些职责,并考虑docker多阶段构build。 Docker多阶段构build的示例

 FROM trion/ng-cli:latest AS ngcli WORKDIR /app COPY . . RUN npm install RUN ng build --prod --aot --progress=false FROM nginx:alpine AS app ENV PORT=8080 EXPOSE 8080 COPY --from=ngcli dist /usr/share/nginx/html/ COPY nginx/default.conf /etc/nginx/conf.d/default.conf.template RUN chown -R nginx /etc/nginx CMD ["/bin/sh","-c",\ "cp /etc/nginx/conf.d/default.conf.template \ /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"] 

nginxconfiguration(来自nginx / default.conf)可能看起来像这样

 server { listen 8080; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri$args $uri$args/ $uri $uri/ /index.html =404; } } 

所以你得到两个不同的容器:一个只是为了构build应用程序,一个只是为了提供应用程序。