.Net 6 Web Api docker 端口转发问题
.Net 6 Web Api docker port forwarding issue
我有一个安装了 swagger 的空 .NET 6 web api 解决方案。我从 Visual Studio 生成了一个 docker 文件,它看起来像这样:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
我创建了一个 docker 图片并推送到我的 docker 中心帐户。
然后我有 运行 这个命令来拉取和 运行 本地图像:
docker container run -d --name mynewapi -p 8000:443 myusername/myreponame:mycontainername
但是当我浏览到 http://localhost:433/swagger/index.html 我得到:
This site can’t be reached localhost refused to connect.
我也曾尝试使用邮递员向示例端点发出 GET 请求,但我遇到了同样的错误。
我做错了什么?
当您 运行 容器时,您将容器中的端口 443 映射到主机上的端口 8000。所以你需要使用端口8000访问它。
由于您映射了 443 端口(即 https 端口),因此您应该使用 https。所以你的URL应该是 https://localhost:8000/swagger/index.html
但这还不够。默认情况下,Swagger 仅在您的解决方案处于 运行 开发模式时可用。您可以使用需要设置为 'Development' 的环境变量 ASPNETCORE_ENVIRONMENT
来控制它。当你 运行 像这样的容器时,你可以在 docker 命令中执行此操作
docker container run -d --name mynewapi -p 8000:443 -e ASPNETCORE_ENVIRONMENT=Development myusername/myreponame:mycontainername
或者您可以像这样将它添加到 Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Development
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
我有一个安装了 swagger 的空 .NET 6 web api 解决方案。我从 Visual Studio 生成了一个 docker 文件,它看起来像这样:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
我创建了一个 docker 图片并推送到我的 docker 中心帐户。
然后我有 运行 这个命令来拉取和 运行 本地图像:
docker container run -d --name mynewapi -p 8000:443 myusername/myreponame:mycontainername
但是当我浏览到 http://localhost:433/swagger/index.html 我得到:
This site can’t be reached localhost refused to connect.
我也曾尝试使用邮递员向示例端点发出 GET 请求,但我遇到了同样的错误。
我做错了什么?
当您 运行 容器时,您将容器中的端口 443 映射到主机上的端口 8000。所以你需要使用端口8000访问它。
由于您映射了 443 端口(即 https 端口),因此您应该使用 https。所以你的URL应该是 https://localhost:8000/swagger/index.html
但这还不够。默认情况下,Swagger 仅在您的解决方案处于 运行 开发模式时可用。您可以使用需要设置为 'Development' 的环境变量 ASPNETCORE_ENVIRONMENT
来控制它。当你 运行 像这样的容器时,你可以在 docker 命令中执行此操作
docker container run -d --name mynewapi -p 8000:443 -e ASPNETCORE_ENVIRONMENT=Development myusername/myreponame:mycontainername
或者您可以像这样将它添加到 Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Development
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]