无法访问本地Docker容器上的Asp.Net Core [英] Cannot acces Asp.Net Core on local Docker Container

查看:92
本文介绍了无法访问本地Docker容器上的Asp.Net Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在asp.net核心中创建了一个应用,并创建了一个dockerfile来生成本地图像并运行它。

I've created a app in asp.net core and create a dockerfile to generate a local image and run it.

FROM microsoft/dotnet:latest

COPY . /app

WORKDIR /app

RUN ["dotnet", "restore"]

RUN ["dotnet", "build"]

EXPOSE 5000/tcp

ENTRYPOINT ["dotnet", "run", "--server.urls", "http://0.0.0.0:5000"]

然后,我使用以下命令构建了图像

Then, I've builded my image with the following command

docker build -t jedidocker/first .

然后,我用以下命令创建了一个容器

Then, I've created a container with the following command

docker run -t -d -p 5000:5000 jedidocker/first

但是当我在主机浏览器中运行以下网址时

But when I run the following url into my host browser

http://localhost:5000/

我有 ERR_EMPTY_RESPONSE

这是网络问题吗?我怎么解决呢?

is it a network problem? How can I solve it?

PS:我的Windows版本是10.0.10586

P.S: My windows version is 10.0.10586

推荐答案

我遇到了同样的问题,并找到了解决方案。您将需要更改默认的监听主机名。默认情况下,应用程序将监听localhost,而忽略来自容器外部的所有传入请求。在 program.cs 中更改代码以侦听所有来电:

I had the same issue and found the solution to this. You will need to change the default listening hostname. By default, the app will listen to the localhost, ignoring any incoming requests from outside the container. Change your code in program.cs to listen for all incoming calls:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://*:5000")
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

更多信息:

https:/ /medium.com/trafi-tech-beat/running-net-core-on-docker-c438889eb5a#.hwhoak2c0

请确保您正在运行带有 -p 标志并带有端口绑定的容器。

Make sure you are running the container with the -p flag with the port binding.

docker run -p 5000:5000 <containerid>

在撰写本文时,似乎

EXPOSE 5000:5000

命令不与 -p 命令具有相同的效果。我也没有在-P(大写)上取得真正的成功。

command does not have the same effect as the -p command. I haven't had real success with -P (upper case) either.

编辑8/28/2016:

Edit 8/28/2016:

需要注意的一件事。如果使用的是-d(分离)模式的docker compose,则 dotnet run 命令可能需要一些时间才能启动服务器。在执行完命令(及其之前的其他命令)之前,您将在 Chrome 中收到 ERR_EMPTY_RESPONSE

One thing to note. If you are using docker compose with the -d (detached) mode, it may take a little while for the dotnet run command to launch the server. Until it's done executing the command (and any other before it), you will receive ERR_EMPTY_RESPONSE in Chrome.

这篇关于无法访问本地Docker容器上的Asp.Net Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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