访问在 Docker 容器中运行的 Angular 应用程序 [英] Access Angular app running inside Docker container
问题描述
我创建了一个 docker 镜像来测试一个 Angular 应用程序,但我无法从主机连接到 docker 容器内正在运行的应用程序.
I've created a docker image to test an Angular app but I cannot connect from host to running app inside docker container.
图像是使用 Dockerfile 创建的:曝光 4200 8080 80
The image was created using a Dockerfile with: EXPOSE 4200 8080 80
我使用以下命令运行 docker 容器:docker run -ti -p 4200:4200 angulardev/bin/bash
I run the docker container with command: docker run -ti -p 4200:4200 angulardev /bin/bash
在容器内部,我创建了 Angular 应用程序并使用以下命令启动它:服务
Inside container I create the Angular application and start it using: ng serve
从容器中,如果我打开 localhost:4200 我会看到应用程序,但我无法从主机操作系统 (RHEL7) 访问它
From container, if I open localhost:4200 I see the application but I cannot access it from host OS (RHEL7)
怎么了?Angular 应用程序从端口 4200 开始,该端口公开并映射到主机 4200.
What is wrong? The Angular app starts on port 4200 which is exposed and mapped to host 4200.
谢谢.
推荐答案
如果你在容器中运行 Angular 应用,只使用 ng serve
它将不允许其他主机访问它.
If you are running angular app inside your container with just ng serve
it won't allow other hosts from accessing it.
您应该使用 ng serve --host 0.0.0.0 --port 4200
这篇关于访问在 Docker 容器中运行的 Angular 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!