我有一个 Azure 应用程序服务“用于 docker 容器的 Web 应用程序”,它需要在 Web 应用程序上线之前运行实体框架数据库迁移。 MS Documentation(here too)表示 Web 应用程序不应启动迁移。所以我假设迁移必须来自 docker 图像中的运行时命令......包含 Web 应用程序的相同 docker 图像。我说“运行时”是因为我的 Azure ARM 部署利用了 github CI/CD“操作”,它在部署时没有迁移功能。无论如何,相应的多阶段 dockerfile 正确生成了我需要的
migration.sql
文件,但我不确定随后如何安装和执行必要的 SqlCmd.exe
(在运行时)运行之前的 migration.sql
文件作为参数。
下面是我现在的dockerfile(针对Windows Server,不是Linux),最初是Visual Studio 2022为我生成的。我做的主要修改是安装“dotnet EF”迁移工具,以及构建命令迁移脚本。
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-windowsservercore-ltsc2022 AS build
RUN dotnet tool install --global dotnet-ef
#ENV PATH="$PATH:/root/.dotnet/tools"
WORKDIR /src
COPY ["Alereem.WebAPI/Alereem.WebAPI.csproj", "Alereem.WebAPI/"]
COPY ["Alereem.Data/Alereem.Data.csproj", "Alereem.Data/"]
RUN dotnet restore "Alereem.WebAPI/Alereem.WebAPI.csproj"
COPY . .
WORKDIR "/src/Alereem.WebAPI"
RUN dotnet build "Alereem.WebAPI.csproj" -c Release -o /app/build
RUN dotnet ef migrations script -p ../Alereem.Data/Alereem.Data.csproj -s ./Alereem.WebAPI.csproj -o /app/publish/migrations.sql
FROM build AS publish
RUN dotnet publish "Alereem.WebAPI.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# HOW DO I ADD AN INSTALLATION OF 'SQLCMD.EXE'?
# AND HOW DO I ADD THE INVOCATION OF SQLCMD, ALLOWING
# FOR THE DB CONNECTION STRING TO BE PASSED AT RUN-TIME
# AS AN ENVIRONMENT VARIABLE?
# ...<RUN SQLCMD.EXE MIGRATION HERE>...
# THEN RUN MAIN APPLICATION...
ENTRYPOINT ["dotnet", "Alereem.WebAPI.dll"]
我在最后添加的 dockerfile 注释代表了我遇到的问题。我发现在线文档建议我可以使用已经安装了
sqlcmd
的罐装基础图像,但我认为不幸的是我必须将基础图像保留在 Visual Studio 为我的 Azure App Service 应用程序创建的基础图像之上。此外,根据 this SO post,我了解到在 dockerfile 中运行两个运行时可执行文件不是“正常”的。
此时,我需要执行以下操作之一:
关于最后一点:同样,我正在为我的 CI/CD 使用 github“actions”。我不知道 github 构建代理连接到我的 Azure 租户中的目标数据库的方法。即使有办法实现这一点,我也需要通过简单地执行我的 Azure ARM 模板来进行complete 部署。这意味着我无法选择手动编辑 github“actions”文件来支持部署。这就是为什么我似乎只能选择让 docker 图像负责导致迁移......一旦它在运行时启动。
指导将不胜感激。