【问题标题】:How to deploy aspnet core app on linx docker container without getting 404 error如何在 linux docker 容器上部署 asp net core 应用程序而不会出现 404 错误
【发布时间】:2021-09-25 12:30:49
【问题描述】:

我有一个 ASPNET Core 5.0 Web api 应用程序在 Windows 本地成功运行,但在部署到托管在 linux 服务器上的 linux docker 容器时失败。导航到 http://internal.apps.talquinelectric.com/is/api 时不会加载页面。我收到 404 错误。

launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:80",
      "sslPort": 80
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "FixBatchAPI": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://internal.apps.talquinelectric.com/is/api",
      "applicationUrl": "http://internal.apps.talquinelectric.com/is/api",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

Startup.cs

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "FixBatchAPI v1"));
            }

            //app.UseHttpsRedirection();
                        
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

Dockerfile

# syntax=docker/dockerfile:1
  FROM mcr.microsoft.com/dotnet/aspnet:5.0
  COPY FixBatchAPI/net50/ App/
  WORKDIR /App
  ENTRYPOINT ["dotnet", "FixBatchAPI.dll"]

docker-compose.yml

version: "3"

services:
   fixbatch-api:
      image: "fixbatch-api-build:latest"
      container_name: "fixbatch-api"
      networks:
        - traefix_proxy
      labels:
        - "traefik.enable=true"
        - "traefik.docker.network=traefik_proxy"
 
        - "traefik.http.routers.fixbatch-api.entrypoint=https"
        - "traefik.http.routers.fixbatch-api.tls=true"
        - "traefik.http.routers.fixbatch-api.rule=Host(`internal.apps.talquinelectric.com`) && PathPrefix(`/is/api`)"
        - "traefik.http.services.fixbatch-api-service.loadbalancer.server.port=80"
        - "traefik.http.middlewares.fixbatch-api-middlewares.chain.middlewares=fixbatch-api-strip"
        - "traefik.http.middlewares.fixbatch-api-strip.stripprefix.prefixes=/is/api"

        - "traefik.http.routers.fixbatch-api-http.entrypoint=http"
        - "traefik.http.routers.fixbatch-api-http.rule=Host(`internal.apps.talquinelectric.com`) && PathPrefix(`/is/api`)"
        - "traefik.http.middlewares.fixbatch-api-redirect-https.redirectscheme.scheme=https"
        - "traefik.http.routers.fixbatch-api-http.middlewares=fixbatch-api-redirect-https"

        - "traefik.http.middlewares.cors.headers.accesscontrolallowmethods=GET,OPTIONS,PUT"
        - "traefik.http.middlewares.cors.headers.accesscontrolalloworigin=*"
        - "traefik.http.middlewares.cors.headers.addvaryheader=true"

networks:
   traefik_proxy:
      external: true
   default:
      external: false

docker-compose 日志的结果:

在浏览器中查看时得到的结果:

【问题讨论】:

  • 从 ASP.NET 应用程序中的日志来看,请求似乎没有通过它。当你在Windows上成功运行的时候,你前面是不是也有Traefik了?
  • (talquinelectric - 有趣的一堆。)
  • Hans,在 Windows 本地运行时,应用程序没有在 Traefik 后面运行。

标签: docker asp.net-core kestrel


【解决方案1】:

添加如下代码,即可打开swaggerui页面。

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "FixBatchAPI v1"));
} 
// default, no below code
else {
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "FixBatchAPI v1"));
}

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 2012-09-26
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多