【问题标题】:.NET Core 3.0 Web API won't start in Docker container.NET Core 3.0 Web API 不会在 Docker 容器中启动
【发布时间】:2020-04-04 21:31:14
【问题描述】:

我刚接触 Docker,试图在我的机器上运行一个概念验证容器。我使用命令dotnet new webapi 生成了一个.NET Core 3.0 Web API 项目。我添加了一个控制器端点,它只返回一个字符串“Hello World”。

using System;
using Microsoft.AspNetCore.Mvc;

namespace DockerHelloWorld.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ValuesController : ControllerBase
    {

        [HttpGet]
        public string Get()
        {
            return "Hello World!";
        }
    }
}

我能够分别使用dotnet builddotnet run 成功构建和运行这个项目。

我现在正试图让它在 Docker 容器中运行。这是我的 Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 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 --framework netcoreapp3 --configuration Release --output out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerHellWorld.dll"]

这是从here复制和稍微修改的。

我还有以下 .dockerignore,以防万一:

bin\
obj\

*.md
*.png

我运行以下命令:

docker build -t dockerhelloworldimage .

docker create -p 3000:80 --name dockerhelloworldcontainer dockerhelloworldimage

docker start dockerhelloworldcontainer

似乎每个步骤都是根据docker build 的输出运行的。在docker create 之后返回一个哈希,在docker run 之后返回“dockerhelloworldcontainer”。

容器立即停止。我通过使用docker container ls -a 命令看到了这一点。

docker logs dockerhelloworldcontainer 具有以下输出:“找不到任何已安装的 .NET Core SDK。您的意思是运行 .NET Core SDK 命令吗?从:https://aka.ms/dotnet-download 安装 .NET Core SDK”但我真的不知道如何理解。

值得一提的是,我在使用 .NET Core 2.2 应用时得到了与此非常相似的东西。

有什么想法吗?我可能会错过什么?

【问题讨论】:

  • 我看到这里的 dll 名称有错字ENTRYPOINT ["dotnet", "DockerHellWorld.dll"] 它应该是DockerHelloWorld 并且与您的项目名称匹配。

标签: docker .net-core


【解决方案1】:

@Zied 在评论中提到的问题是您要从入口点命令调用的程序集名称中的拼写错误。

如果你在 docker 文件上修复它:

# All other content remains the same

ENTRYPOINT ["dotnet", "DockerHelloWorld.dll"]

您将成功启动容器。

列出正在运行的容器:docker ps 显示:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                   NAMES
7dcb5c122445        dockerhelloworldimage   "dotnet DockerHelloW…"   5 minutes ago       Up 4 minutes        0.0.0.0:3000->80/tcp                                    dockerhelloworldcontainer

现在,这看起来像是一条误导性的错误消息,但如果您尝试从 CLI(而不是 docker)实际运行错误的 dotnet 可执行文件:

> dotnet MyNonExistingExeName.dll

您将收到以下消息:

Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-.\bin\Debug\netcoreapp3.0\DockerHellWorld.dll does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

我相信第二条和第三条适用于本案。这意味着如果您有一个 dotnet 可执行文件(或全局/本地工具),您可以使用 dotnet 命令运行它们,因此问题是您的命令被解释为不存在的工具或命令,因此错误。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    运行以下命令在 docker 上部署示例 Dotnet Web API 代码

    docker-compose build
    docker-compose up
    

    Dockerfile

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
    WORKDIR /app
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
    COPY ./SampleWebApi3.0.csproj /SampleWebApi3.0/
    RUN dotnet restore ./SampleWebApi3.0/SampleWebApi3.0.csproj
    COPY . ./SampleWebApi3.0/
    WORKDIR /SampleWebApi3.0/
    RUN dotnet build "SampleWebApi3.0.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "SampleWebApi3.0.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "SampleWebApi3.0.dll"]
    

    docker-compose.yml

    version: '3.5'
    
    services:
        sample-web-api:
            build: SampleWebApi3.0/
            restart: always
            ports:
                - "8085:80"
    

    Sample web API code 3.0 along with Dockerfile on Github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 2019-08-28
      • 2018-12-01
      相关资源
      最近更新 更多