无法连接到运行Kestrel服务器的本地Docker

时间:2018-05-27 17:14:54

标签: docker windows-10 kestrel-http-server docker-for-windows

我有一个Kestrel服务器,它将Hello World返回给任何HTTP请求。

static class Program
{
    static void Main(string[] args)
    {
        var webHostBuilder = WebHost.CreateDefaultBuilder(args)
                                    .UseSetting("applicationName", "Hello World")
                                    .Configure(builder => {
                                        builder.Run(async context =>
                                                {
                                                    var textBytes = UTF8.GetBytes("Hello World!");
                                                    await context.Response.Body.WriteAsync(textBytes, 0, textBytes.Length, default (CancellationToken));
                                                });
                                        })
                                        .UseUrls("http://+:8000");
        var webHost = webHostBuilder.Build();
        webHost.Run();
    }
}

我已将构建程序集添加到Docker镜像并使用此DockerFile构建它:

FROM microsoft/dotnet
WORKDIR /app
ADD /application /app
EXPOSE 8000
ENTRYPOINT [ "dotnet", "hello-world-server.dll" ]

我用它来运行它:

>docker run hello-world-server --publish-list 8000:8000

当我向http://localhost:8000发送请求时,我会返回502。

我在Windows 10上使用Windows容器。

构建和完整的输出运行如下:

C:\...\hello-world-server-docker>docker build -t hello-world-server .
Sending build context to Docker daemon  84.99kB
Step 1/5 : FROM microsoft/dotnet
 ---> d08db1d19023
Step 2/5 : WORKDIR /app
Removing intermediate container 873dea47b78b
 ---> de4b80a52d54
Step 3/5 : ADD /application /app
 ---> ba75fe5b5efc
Step 4/5 : EXPOSE 8000
 ---> Running in 1ac9c977c9b4
Removing intermediate container 1ac9c977c9b4
 ---> 22cb3848d762
Step 5/5 : ENTRYPOINT [ "dotnet", "hello-world-server.dll" ]
 ---> Running in 17f3b01f6ed0
Removing intermediate container 17f3b01f6ed0
 ---> 82c7e3aadfc2
Successfully built 82c7e3aadfc2
Successfully tagged hello-world-server:latest

C:\...\hello-world-server-docker>docker run hello-world-server --publish-list 8000:8000
Hosting environment: Production
Content root path: C:\app
Now listening on: http://[::]:8000
Application started. Press Ctrl+C to shut down.

当向localhost:8000发出请求时,来自Kestrel的控制台上没有进一步的输出,而通常会有普通的控制台应用程序。

我也试过用>docker run hello-world-server --publish 8000:8000运行它。

1 个答案:

答案 0 :(得分:1)

我认为问题出在docker run命令中。 --publish-list 8000:8000就像现在一样,是容器入口点的参数。

运行容器并公开端口的命令是:

docker run -p 8000:8000 hello-world-server

docker run的每个命令行选项必须放在之前图像名称。 图像名称后的所有内容都是容器本身的命令。