dotnet/aspnetcore

"UseKestrel" does not work in docker.

ANIZA15 opened this issue · 1 comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I just started Windows 11 and it says, "The port is already in use." error.

Expected Behavior

No response

Steps To Reproduce

  1. Contents of Program.cs.
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Server.Kestrel.Core;

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(options =>
{
     // HTTP/2 Only のエンドポイント(not HTTPS)
     try{
        options.Listen(IPAddress.Parse("0.0.0.0"), 5010,
        listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; });
     }
     catch(Exception e){
         Console.WriteLine(e.Message);
     }

    try{
         // 疎通確認用のHTTP 1.1エンドポイントの設定
    options.Listen(IPAddress.Parse("0.0.0.0"), 5012,
        listenOptions => { listenOptions.Protocols = HttpProtocols.Http1; });
    }
     catch(Exception e){
         Console.WriteLine(e.Message);
     }

   
});

builder.Services.AddGrpc();
builder.Services.AddMagicOnion();

var app = builder.Build();

// 疎通確認用のルーティング
app.MapGet("/", () => "Hello World!");

// MagicOnion用のルーティング
app.MapMagicOnionService();

app.Run();
  1. Dockerfile and docker-compose contents.
    Dockerfile
# Learn about building .NET container images:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG TARGETARCH
WORKDIR /source

# copy csproj and restore as distinct layers
COPY aspnetapp/*.csproj .
RUN dotnet restore -a $TARGETARCH

RUN dotnet add package Grpc.AspNetCore
RUN dotnet add package MagicOnion.Server

# copy and publish app and libraries
COPY aspnetapp/. .
RUN dotnet publish -o /app


# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
# EXPOSE 8080
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["./aspnetapp"]

docker-compose.yml

version: '3.8'
services:
  api:
    build: 
      context: .
      dockerfile: Dockerfile
    ports:
      - 5010:8080

3.Run docker-compose up -d.

4.I just started Windows 11 and it says, "The port is already in use." error.

2024-05-18 23:00:50 warn: Microsoft.AspNetCore.Server.Kestrel[0]
2024-05-18 23:00:50       Overriding address(es) 'http://*:8080'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
2024-05-18 23:00:50 fail: Microsoft.Extensions.Hosting.Internal.Host[11]
2024-05-18 23:00:50       Hosting failed to start
2024-05-18 23:00:50       System.IO.IOException: Failed to bind to address http://127.0.0.1:5010: address already in use.
2024-05-18 23:00:50        ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
2024-05-18 23:00:50        ---> System.Net.Sockets.SocketException (98): Address already in use
2024-05-18 23:00:50          at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
2024-05-18 23:00:50          at System.Net.Sockets.Socket.Bind(EndPoint localEP)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
2024-05-18 23:00:50          --- End of inner exception stack trace ---
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass28_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
2024-05-18 23:00:50       --- End of stack trace from previous location ---
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50          --- End of inner exception stack trace ---
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.EndpointsStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(ListenOptions[] listenOptions, AddressBindContext context, Func`2 useHttps, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
2024-05-18 23:00:50          at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
2024-05-18 23:00:50          at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
2024-05-18 23:00:50 Unhandled exception. System.IO.IOException: Failed to bind to address http://127.0.0.1:5010: address already in use.
2024-05-18 23:00:50  ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
2024-05-18 23:00:50  ---> System.Net.Sockets.SocketException (98): Address already in use
2024-05-18 23:00:50    at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
2024-05-18 23:00:50    at System.Net.Sockets.Socket.Bind(EndPoint localEP)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
2024-05-18 23:00:50    --- End of inner exception stack trace ---
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass28_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
2024-05-18 23:00:50 --- End of stack trace from previous location ---
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50    --- End of inner exception stack trace ---
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.EndpointsStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(ListenOptions[] listenOptions, AddressBindContext context, Func`2 useHttps, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
2024-05-18 23:00:50    at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
2024-05-18 23:00:50    at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
2024-05-18 23:00:50    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
2024-05-18 23:00:50    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
2024-05-18 23:00:50    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
2024-05-18 23:00:50    at Program.<Main>$(String[] args) in /source/Program.cs:line 53

Exceptions (if any)

No response

.NET Version

8.0.204

Anything else?

.NET SDK:
Version: 8.0.204
Commit: c338c7548c
Workload version: 8.0.200-manifests.7d36c14f

runtime env:
OS Name: Windows
OS Version: 10.0.22635
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.204\