当应用程序在 Docker 容器中运行时,Swagger UI 给出 ​​404 [英] Swagger UI gives 404 when app is running in Docker container

查看:90
本文介绍了当应用程序在 Docker 容器中运行时,Swagger UI 给出 ​​404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我在 Startup.cs 中设置了 Swagger 和 Swashbuckle:

In my project I have set up Swagger with Swashbuckle like so in my Startup.cs:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Example API", Version = "v1" });
            });

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

当我从 Visual Studio 运行我的应用程序时,我可以访问 localhost:123456/swagger 并获取 API 文档,就像它应该的那样.但是,当我使用 docker-compose up 通过 Docker 运行我的应用程序并访问 example_IP/swagger 时,我发现 404 未找到.用 Postman 测试 example_IP 给了我预期的数据,所以 IP 或后端没有问题.我尝试了几个不同的 URL,/swagger/swagger-ui.html 等,但没有区别.当在 Docker 容器中运行时,是否有任何特定的配置必须写入 Dockerfile 或其他地方才能访问 Swagger 文档?我正在通过 nginx 反向代理运行我的后端,不确定这是否重要.

When I run my application from Visual Studio I can access localhost:123456/swagger and get the API documentation, like it should be. However when I run my application through Docker with docker-compose up and access example_IP/swagger I get a 404 not found. Testing the example_IP with Postman gives me the expected data, so nothing wrong with the IP or the backend. I have tried with several different URLs, /swagger, /swagger-ui.html etc, but no difference. Are there any specific configurations that must be written in the Dockerfile or elsewhere to be able to access the Swagger documentation when running in a Docker container? I am running my backend through an nginx reverse proxy, not sure if that matters or not.

Docker-compose 文件:

Docker-compose file:

version: "3.7"
services:
  dotnet-backend:
    container_name: dotnet-backend
    build: .
   #expose:
    #  - "80"
    links:
      - mssql-db
    ports:
      - "8000:80"
  nginx-reverse-proxy:
    container_name: nginx-reverse-proxy
    build: ./nginx/
    #command: tail -F /dev/null
    command: nginx -g 'daemon off;'
    ports:
      - "80:80"
    #  - "443:443"
    expose:
      - "80"
      - "443"
    links:
      - dotnet-backend
  mssql-db:
    container_name: mssql-db
    #image: mcr.microsoft.com/mssql/server:2019-latest
    build: ./Database
    environment: 
      - SA_PASSWORD=password
      - ACCEPT_EULA=Y
    volumes:
     - ./Database/:/scripts/
    ports:
      - "1433:1433"
    expose:
      - "1433"
    command:
      - /bin/bash
      - -c 
      - |

Dockerfile:

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Debug -o out Project.csproj

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .

EXPOSE 80

ENTRYPOINT ["dotnet", "ProjectApi.dll", "--environment=Development"]

推荐答案

我怀疑这对最初的问题有帮助,但我只是打了这个.既然 WebApi 模板内置了 Swagger,那么在通过手动步骤 (docker run) 或 Visual Studio 运行容器时可能会很棘手.

I doubt this will help the original question, but I just hit this. Now that the WebApi templates have Swagger built-in, it can be tricky when running a container via manual steps (docker run) or Visual Studio.

Swagger UI 现在处于开发"阶段默认阻止:

The Swagger UI is now in a "Development" block by default:

if (env.IsDevelopment())
{
  app.UseDeveloperExceptionPage();
  app.UseSwagger();
  app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "test v1"));
}

这在 Visual Studio 的 Docker 工具中运行良好,但如果您在不进行任何更改的情况下构建 Dockerfile,它将构建为 Release,并且 Swagger 将无法访问.

This works fine in Visual Studio's Docker tools, but if you build the Dockerfile without making changes, it's built as Release, and Swagger won't be accessible.

因此,如果您希望 Swagger 仍然可用,这只是将容器投入生产时遇到的一个问题.

So, this is just one gotcha when moving a container into production if you want Swagger still available.

解决方法是将两条 swagger 行移出 env.IsDevelopment() 条件.

The fix is to move the two swagger lines out of the env.IsDevelopment() conditional.

这篇关于当应用程序在 Docker 容器中运行时,Swagger UI 给出 ​​404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆