代码之家  ›  专栏  ›  技术社区  ›  Loading

如何在docker中公开角度应用程序的端口

  •  0
  • Loading  · 技术社区  · 6 年前

    我想启动一个docker图像,然后进入浏览器,角度应用程序应该是开箱即用的。

    这是我的docker文件:

    FROM node_net_angular
    WORKDIR /source
    
    COPY . .
    
    EXPOSE 4200
    
    RUN npm install
    
    ENTRYPOINT ["ng", "serve"]
    

    我愿意

    docker build -t test01 . 
    

    docker run  test01 
    

    控制台显示我打开localhost:4200,但这没有显示任何内容。

    有没有一步我忘了?

    编辑:

    我可以直接进入文件夹,输入ng-serve,一切正常。我把所有东西都直接复制到我的docker图像中。运行ng service也不会产生任何错误。

    docker run -p 4200:4200 test01 
    

    但这也没有显示网站

    编辑:我发现了问题。问题是入口点。如果你这样做

    CMD ng serve --host 0.0.0.0
    

    它工作得很好

    1 回复  |  直到 6 年前
        1
  •  0
  •   Guerric P    6 年前

    你不应该用 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 保存证书配置的文件夹。