首页 > 解决方案 > Docker 运行命令抛出和错误消息

问题描述

我正在使用 docker 构建和运行我的 .Net Core 3.1 控制台应用程序。这是一个简单的 Hello World 应用程序:

using System;

namespace DockerTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

这是我的码头文件:

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY . .
ENTRYPOINT ["dotnet", "DockerTesting"]

它也很简单,只需获取基础映像并将应用程序复制到 WORKDIR 中。我使用 Powershell 构建和运行映像。

PS ~: docker build -t testingdocker .
Sending build context to Docker daemon  7.168kB
Step 1/4 : FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
 ---> 6bb83e9aa359
Step 2/4 : WORKDIR /src
 ---> Running in 4c3319712cf8
Removing intermediate container 4c3319712cf8
 ---> cf1ab373e085
Step 3/4 : COPY . .
 ---> 1c6a846459d8
Step 4/4 : ENTRYPOINT ["dotnet", "DockerTesting"]
 ---> Running in 56e335f09bc2
Removing intermediate container 56e335f09bc2
 ---> 2ef84c282ac0
Successfully built 2ef84c282ac0
Successfully tagged testingdocker:latest
PS ~: docker run testingdocker

尝试运行图像后,我收到以下错误消息:

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-DockerTesting 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.

但是当我通过 VS 运行应用程序时,它就像一个魅力。

标签: docker.net-core

解决方案


请查看Docker 文档中的.NET Core 应用程序示例 Dockerfile 。

在您的 Dockerfile 中,您缺少构建(和发布部分),这通常希望作为 Docker 构建过程的一部分来完成。此外,您.dllENTRYPOINT.

尝试用类似的东西替换你的(可能需要调整一些路由,这假设 Dockerfile 位于 .csproj 级别并且项目没有其他依赖项):

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# Copy .csproj and restore
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and publish
COPY . ./
RUN dotnet publish -c Release -o out


FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerTesting.dll"]

推荐阅读