问题描述
我已经创建了一个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应用程序并使用以下命令启动它:ng服务
Inside container I create the Angular application and start it using:ng serve
从容器中,如果我打开localhost:4200,我会看到该应用程序,但无法从主机OS(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.
谢谢.
推荐答案
如果仅使用ng serve
在容器内运行angular app,则将不允许其他主机访问它.
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应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!