无法从 docker 容器访问 angular2 应用程序 [英] not able to access angular2 app from docker container

查看:25
本文介绍了无法从 docker 容器访问 angular2 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用官方节点镜像创建了一个 docker 容器.

I created a docker container using official node image.

docker pull node

然后我使用 angular-cli 创建了一个 angular 2 应用程序.

then I created an angular 2 app using angular-cli.

ng new docker-demo

然后我使用以下命令从我的 angular2 应用程序的目录运行我的 docker 容器:

then I ran my docker container from the directory of my angular2 app using following command:

cd docker-demo

&然后在目录中:

& then inside the directory :

docker run -p 8080:4200 -v $(pwd):/var/www -w "/var/www" node npm start

其中 pwd 代表我的 angular2 应用程序的目录.

where pwd stands for directory of my angular2 app.

现在在我的控制台中,我可以看到我的 angular2 应用程序被捆绑了 &就像在我的本地机器上运行一样.

now in my console I can see my angular2 app getting bundled & running just like it runs on my local machine.

但是当我尝试使用我的 docker-machine ip 从我的浏览器访问它时:

but when I try to access it from my browser using ip of my docker-machine :

http://192.168.99.100:8080/

它不起作用.

PS:angular2 应用在我的本地系统中运行良好.

PS: angular2 app works perfectly fine in my local system.

是否需要额外的配置?

任何输入?

提前致谢.

推荐答案

我遇到了类似的问题.能够使用 ng serve --host 0.0.0.0 作为我的容器的启动命令来修复它.记住在构建时暴露正确的端口很重要.确保使用 EXPOSE 4200 或任何其他要在 Dockerfile 中公开应用程序的端口.4200 是 Angular2/4 应用的默认值,所以大多数时候是最好的选择.

I had a similar problem. Was able to fix it using ng serve --host 0.0.0.0 as the starting command for my container. It is important to remember exposing the right ports at build time. Make sure you use EXPOSE 4200 or any other port you want to expose your app in your Dockerfile. 4200 is the default for Angular2/4 apps, so most of the time is the best choice.

如果你使用 Docker compose,同样的事情.请记住公开端口并在 ng serve 命令中使用适当的参数.希望它可以帮助您和任何其他遇到同样问题的船长.

If you're using Docker compose, the same thing. Remember to expose the port and use the appropriate arguments in the ng serve command. Hope it helps you and any other fellow captain struggling with the same problem.

这篇关于无法从 docker 容器访问 angular2 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆