最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I serve an Angular 5 app without using ng serve? - Stack Overflow

programmeradmin5浏览0评论

I can build and serve an Angular 5 project with ng serve and view it in my browser. I'd like to move the files onto a docker container because my final app needs to have a php backend.

When I move the same file system that runs with ng serve to a docker build and try to navigate to it, I receive a server error.

I know the container works properly because I can serve PHP files to the browser at the respective localhost port without any issues. So it needs to be something with Angular that is causing the error.

I've noticed that the ng new angular-tour-of-heroes project doesn't have an index.html in the root project directory. When I move the one in the src/ folder to the root, I still get nothing in the browser.

How can I serve an Angular app using Docker instead of ng serve?

I can build and serve an Angular 5 project with ng serve and view it in my browser. I'd like to move the files onto a docker container because my final app needs to have a php backend.

When I move the same file system that runs with ng serve to a docker build and try to navigate to it, I receive a server error.

I know the container works properly because I can serve PHP files to the browser at the respective localhost port without any issues. So it needs to be something with Angular that is causing the error.

I've noticed that the ng new angular-tour-of-heroes project doesn't have an index.html in the root project directory. When I move the one in the src/ folder to the root, I still get nothing in the browser.

How can I serve an Angular app using Docker instead of ng serve?

Share Improve this question asked Dec 21, 2017 at 17:54 KnightKnight 5767 silver badges21 bronze badges 1
  • 2 If you run ng-build, it will create a dist directory. If you navigate to the index.html file from that directory it should work – user184994 Commented Dec 21, 2017 at 18:03
Add a ment  | 

1 Answer 1

Reset to default 8

This answer will be two parts since it sounds like you may not be familiar with the build process.

Building

This is more general to most JavaScript frameworks. There is usually a "build" process that will produce the static web application in some way that it can be served by a web server.

In the case of Angular, it is ng build. You can learn more about the mand at https://github./angular/angular-cli/wiki/build. The ng build mand would create a dist directory in your project where the built HTML, CSS< and JavaScript lives. The files in this directory are what you would push to your web server to serve.

Docker

Now that we understand how to get the source of the web application to serve, we want to run it as a container. Based on your question, I am assuming that you already have a Docker image with a web server. In this case, you could copy the dist folder to the same location as the existing Dockerfile that builds your web server and add a line to your Dockerfile such as:

COPY dist/* /my/webserver/root

If you can provide more information about your existing image, Dockerfile, and environment I could provide a better example of building and producing the Angular application to the final web server image.

Though, you don't necessarily need to serve the Angular application from your PHP application. If your Angular application is connecting to the PHP application they could still be separate Docker images and containers that are linked together.

发布评论

评论列表(0)

  1. 暂无评论