ASP .NET 7 上有一个 Web 应用程序,其中安装了实体框架依赖项(文件WebApplication.csproj
):
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
</ItemGroup>
</Project>
该应用程序在 Docker 容器中运行(文件内容Dockerfile
):
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["WebApplication.csproj", "./"]
RUN dotnet restore "WebApplication.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "WebApplication.csproj" -c Release -o /app/build
# Добавляем Entity Framework CLI в образ
RUN dotnet tool install --global dotnet-ef --version 7.0.10
ENV PATH "$PATH:/root/.dotnet/tools"
FROM build AS publish
RUN dotnet publish "WebApplication.csproj" -c Release -o /app/publish /p:UseAppHost=false
# Окончательный образ для запуска приложения
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
正如您在文件中看到的,当我们构建映像时,我们设置了dotnet-ef
. 接下来,我们在帮助下docker compose up
启动容器。
问题是,如果我进入容器 ( docker exec -it <container-id> /bin/bash
) 并尝试执行命令dotnet ef
(或任何其他命令),我会收到错误:
The command could not be loaded, possibly because:
* You intended to execute a .NET application:
The application 'dotnet-ef' does not exist.
* You intended to execute a .NET SDK command:
No .NET SDKs were found.
Download a .NET SDK:
https://aka.ms/dotnet/download
Learn about SDK resolution:
https://aka.ms/dotnet/sdk-not-found
尝试Dockerfile
添加行:
# Установка .NET SDK для Entity Framework
RUN apt update && \
apt install -y --no-install-recommends apt-transport-https && \
apt install -y dotnet-sdk-7.0
但我收到一条消息,指出dotnet-sdk-7.0
未找到存储库:
...
5.703 Reading package lists...
6.579 Building dependency tree...
6.766 Reading state information...
6.927 E: Unable to locate package dotnet-sdk-6.0
6.927 E: Couldn't find any package by glob 'dotnet-sdk-7.0'
------
failed to solve: process "/bin/sh -c apt update && apt install -y --no-install-recommends apt-transport-https && apt install -y dotnet-sdk-7.0" did not complete successfully: exit code: 100
如果我尝试安装 .NET SDK 版本不是 7.0,而是 6.0,也会出现同样的问题。也许有人有如何在 Docker 容器中使用实体框架的工作配置?因为我不知道我的方向是否正确,我的错误在哪里,哪里需要纠正。我将不胜感激任何帮助。