我用脚手架制作了一个 ASP.NET API。我在 Visual Studio 2019 中生成了一个 docker 文件:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["CoinAPI.csproj", "CoinAPI/"]
RUN dotnet restore "CoinAPI/CoinAPI.csproj"
WORKDIR "/src/CoinAPI"
COPY . .
RUN dotnet build "CoinAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CoinAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoinAPI.dll"]
我正在使用命令构建项目:
docker build -t coinapi .
并运行它:
docker run -dp 12344:12344 coinapi
容器在 Windows 的 docker desktop 中运行,但是当我访问 localhost:12344
时出现错误 404。添加这行代码后 webBuilder.UseUrls("http://localhost:12344");
在 Program.cs 文件中,我在访问 URL 时收到 ERR_EMPTY_RESPONSE。我该怎么做才能成功访问在 docker 容器中运行的 Swagger API?
最佳答案
应用默认监听80
和443
端口,试试这个
docker run -dp 12344:80 coinapi
您应该使用 http://localhost:12344
关于asp.net - 如何访问在 Docker 中发布的 ASP.NET Swagger API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67431299/