powershell - Docker-无法使用Powershell运行ASP.NET Core API,但可以在Visual Studio中运行

标签 powershell docker

我正在尝试在Docker容器中运行ASP.net核心API。从Visual Studio运行可以很好地运行,但是使用以下powershell命令运行该API时我无法访问该API:

docker run --name test6 --rm -it -p 9001:80 webapplication6:dev

它只是返回:
root@874b63595efc:/app#

我希望它会告诉我它已开始侦听给定的端口。我尝试使用Docker Hub中的现有存储库进行测试:
docker run --name aspnetcore_sample --rm -it -p 8000:80 mcr.microsoft.com/dotnet/core/samples:aspnetapp

它成功返回了以下内容:
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

它可以在我的本地主机中访问。回到我的测试API项目,我在Dockerfile中有以下文本:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY ["WebApplication6.csproj", ""]
RUN dotnet restore "./WebApplication6.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication6.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication6.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication6.dll"]

这是Visual Studio自动添加的。

这是docker ps返回的输出

docker ps result

我的测试应用程序的命令值为“bash”。我是否需要配置其他任何东西才能单独使用Powershell而不使用Visual Studio来运行API容器?

最佳答案

似乎docker引擎正在忽略Dockerfile中的entrypoint指令,因为您可以看到正在运行的容器中命令为null。

您可以通过在Dockerfile中指定CMD指令来解决此问题。尝试将这一行添加到Dockerfile的底部,看看是否有帮助:

CMD ["dotnet", "WebApplication6.dll"]

此外,请确保您的Docker镜像是使用当前版本的Dockerfile构建的,您可以尝试使用此命令来验证以下内容:
docker build -t webapplication6:dev .    

关于powershell - Docker-无法使用Powershell运行ASP.NET Core API,但可以在Visual Studio中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58585879/

相关文章:

windows - 来自 System.Net.WebClient 的 powershell 字符编码

powershell - 后台作业似乎没有运行

docker - 从 Docker 容器内部,如何连接到机器的本地主机?

Docker 组合部署

powershell - Powershell脚本将数组内容导出到Excel

powershell - 创建Windows Docker容器时出现错误

excel - 从 Excel 中提取列

python - 无法在 Docker 上运行 Flask (ModuleNotFoundError)

docker - Docker Compose无法打开文件构建:无此类文件或目录

docker - Nginx try_files 不适用于我的 Vue 应用程序