Nguyễn Minh Vương
Nguyễn Minh Vương

Reputation: 369

Asp.net core SSL could not run in docker container

I have Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    var options = new RewriteOptions().AddRedirectToHttps(301, 44339);
    app.UseRewriter(options);

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    var localizedOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(localizedOptions.Value);

    app.UseIdentity();

     app.UseMvc(routes =>
    {
        routes.MapRoute(
            "LocalizedDefault",
            "{lang:lang}/{controller=Home}/{action=Index}/{id?}"
        );

        routes.MapRoute(
            "default",
            "{*catchall}",
            new { controller = "Home", action = "RedirectToDefaultLanguage", lang = "vi" });
    });
}

and

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

    host.Run();
}

I used this cmd:

docker run -it --rm -p 1234:80 --name voiconshop_ps voiconshop

Here is ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                   NAMES
f752d350823b        voiconshop          "dotnet VoiConShop.d…"   About a minute ago   Up 59 seconds       0.0.0.0:1234->80/tcp    voiconshop_ps

And this is dockerfile

FROM microsoft/aspnetcore:1.1
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "VoiConShop.dll"]

I run http://localhost:1234/ then direct to https://localhost:44339/. However, the page return "This site can’t be reached". So what did I do wrongly? Please help me. Thanks you so much. If need more information I will edit this question.

Upvotes: 4

Views: 6361

Answers (1)

Kalaiselvan
Kalaiselvan

Reputation: 129

I see multiple issues in your implementation.

  1. I think running web app/service in HTTPs requires SSL certificate to be bound. Consider the below code snippet to replace .UseKestrel() in your code. Implement LoadCertificate method to return your SSL Certificate(X509Certificate2 object)

     .UseKestrel((options => { options.Listen(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 443),
                  listenOptions =>
                  {
                      var httpsConnectionAdapterOptions = new AspNetCore.Server.Kestrel.Https.HttpsConnectionAdapterOptions()
                      {
                          ClientCertificateMode = AspNetCore.Server.Kestrel.Https.ClientCertificateMode.NoCertificate,
                          SslProtocols = System.Security.Authentication.SslProtocols.Tls,
                          ServerCertificate = LoadCertificate()
                      };
                      listenOptions.UseHttps(httpsConnectionAdapterOptions);
                  }); 
    
  2. Add 443 port in dockerfile. It should look like

    Expose 80 443

  3. We should also set "ASPNETCORE_URLS" Envionment Variable with "http://:80/;https://:443/" in docker-compose.override.yml.

    environment:

    • ASPNETCORE_ENVIRONMENT=Development
    • ASPNETCORE_URLS=http://:80/;https://.443

    ports:

    • "80"
    • "443"

Upvotes: 2

Related Questions