在我的项目中,我在 Startup.cs 中使用 Swashbuckle 设置了 Swagger:
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 运行我的应用程序时docker-compose up
和访问example_IP/swagger
我收到 404 未找到。测试example_IP
使用 Postman 为我提供了预期的数据,因此 IP 或后端没有任何问题。我尝试过几个不同的网址,/swagger
, /swagger-ui.html
等,但没有区别。在 Docker 容器中运行时,是否必须在 Dockerfile 或其他地方编写任何特定配置才能访问 Swagger 文档?我通过 nginx 反向代理运行我的后端,不确定这是否重要。
Docker-compose 文件:
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:
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"]