Docker上的ASP.NET不向浏览器提供Web应用 [英] ASP.NET on Docker Not Serving Web App to Browser

查看:76
本文介绍了Docker上的ASP.NET不向浏览器提供Web应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将Web应用程序打包在Docker中时,我无法将我的ASP.NET Web应用程序提供给浏览器.

I can't get my ASP.NET web application to get served to my browser when the web app is containerized in Docker.

我正在运行Mac,并且已经使用Visual Studio Code创建了ASP.NET Web应用程序.这是一个简单的现成的演示,基于yo aspnet空应用程序".当运行本机"(在Docker外部)时,此应用程序将服务于"Hello World!".到 http://localhost:5000 就可以了.换句话说,运行dnx web将启动Web服务器(Kestrel)并生成:

I'm running a Mac, and I've used Visual Studio Code to create an ASP.NET web application. It's a simple, out-of-the-box demo that is based on the yo aspnet "Empty Application." When run "native" (outside of Docker), this application serves a "Hello World!" to http://localhost:5000 just fine. In other words, running dnx web starts the web server (Kestrel) and yeilds:

Hosting environment: Production 
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

这很好.现在输入Docker.我似乎已经成功构建了一个包含Web应用程序的Docker映像,当我在Docker中运行容器时,我从Kestrel得到了相同的输出.也很好,但是,我无法再加载"Hello World!"我的浏览器中的页面 http://localhost:5000 .相反,我得到一个ERR_CONNECTION_REFUSED.这显然是显而易见的,因为由于Docker的间接"作用,不再有任何直接服务于端口5000的东西.换句话说,我认为转发配置不正确,或者我误解了地址.

This is good. Now enter Docker. I seem to have successfully built a Docker image containing the web application, and when I run the container in Docker, I get the same output from Kestrel. Also good, but, I can no longer load the "Hello World!" page in my browser at http://localhost:5000. Instead, I get a ERR_CONNECTION_REFUSED. This is fairly obviously because due to the Docker "indirection," there is nothing serving directly to port 5000 anymore. In other words, I think there's an incorrect forwarding configuration, or, I think am misunderstanding the addressing.

我认为此过程涉及端口转发.在我的Dockerfile中,我使用的是EXPOSE 5000,我认为这允许我使用如下运行命令将端口5000的本地使用映射到Docker容器的端口5000:

I believe that port forwarding is involved in this process. In my Dockerfile, I am using an EXPOSE 5000 which I thought would allow me to map my local use of port 5000 to the Docker container's port 5000 using a run command like this:

docker run -i -t -p 5000:5000 container_name

但是 http://localhost:5000 (ERR_CONNECTION_REFUSED)并非如此.因此,我想到Docker几乎肯定不在localhost位置.我注意到Docker加载时说:

But that's not the case with http://localhost:5000 (ERR_CONNECTION_REFUSED). So it occurred to me that Docker is almost certainly not at localhost. I had noticed when Docker loads, it says:

docker is configured to use the default machine with IP 192.168.99.100

所以,我想我会尝试 http://192.168.99.100:5000 ,但是再次(令人困惑?)ERR_CONNECTION_REFUSED.接下来,我在此处读了一篇有趣的文章,我能够从建议的命令中确定

So, I thought I'd try http://192.168.99.100:5000, but again (confusingly?) ERR_CONNECTION_REFUSED. Next, I read an interesting article here and I was able to determine from the suggested command

docker inspect container_name | grep IPAddress

为容器分配了"IPAddress": "172.17.0.2"

所以,我想我会尝试 http://172.17.0.2:5000 .现在我们实际上可能正在到达某个地方,因为我不是使用ERR_CONNECTION_REFUSED而是使用了旋转的沙漏并导致了超时.但是仍然没有"Hello World!"

So, I thought I'd try http://172.17.0.2:5000. And now we might actually be getting somewhere, because instead of a ERR_CONNECTION_REFUSED, I instead get a spinning hourglass and a resulting timeout. But still no "Hello World!"

我可能会缺少什么?

推荐答案

事实证明,网络应用程序 在怀疑的虚拟机192.168.99.100的IP地址上可用. 172.17.0.2显然是某种红色的鲱鱼.

It turns out that the web application is available at the IP address of the virtual machine 192.168.99.100 as suspected. 172.17.0.2 was clearly some sort of red herring.

真正的推动者似乎是容器的默认内部" IP是0.0.0.0

The real kicker seems to be that the container's default "internal" IP is 0.0.0.0

遵循

Following the excellent advice of this posting, I edited the Dockerfile and specified the following:

ENTRYPOINT ["dnx", "web", "--server.urls", "http://0.0.0.0:5000"]

因为...

这将使我们的Web应用程序能够处理来自 Docker提供的端口转发,默认为0.0.0.0

This will allow our web application to serve requests that come in from the port forwarding provided by Docker which defaults to 0.0.0.0

端口映射对于将主机的端口链接到容器的端口至关重要,但是EXPOSE命令显然是多余的.现在,当我跑步

The port mapping is crucial to link the host's port to the container's, but the EXPOSE command is apparently redundant. Now, when I run

docker run -i -t -p 80:5000 container_name

我可以简单地浏览到 http://192.168.99.100 (端口80是隐式的)

I can simply browse to http://192.168.99.100 (port 80 is implicit)

中提琴!有我的"Hello World!"

And viola! There's my "Hello World!"

这篇关于Docker上的ASP.NET不向浏览器提供Web应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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