简体   繁体   中英

Cannot connect to local Docker running Kestrel server

I have a Kestrel server that returns Hello World to any HTTP request.

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();
    }
}

I've added the build assemblies to a Docker image and built it with this DockerFile :

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

I've run it with this:

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

When I send a request to http://localhost:8000 I get a 502 returned.

I'm using Windows containers on Windows 10.


The full output from a build & run is below:

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.

When a request is made to localhost:8000 there's no further output on the console from Kestrel, whereas there usually would be is this were a normal console application.


I've also tried running it with >docker run hello-world-server --publish 8000:8000 .

I think the problem is in the docker run command. --publish-list 8000:8000 as it is right now, is a parameter for the containers entrypoint.

The command to run a container and expose a port is:

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

Every command line option for docker run must be placed before the image name. Everything after the image name is a command for the container itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM