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

查看:86
本文介绍了无法从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.

是否需要任何其他配置?

is there any extra configuration required?

任何输入?

提前谢谢.

推荐答案

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

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天全站免登陆