我创建了一个围绕 Playwright-sharp v0.192.0 包装功能的 .NET Core 3.1 nuget 库。我在 REST API(也是 .NET Core 3.1)中使用这个库,并且在本地一切正常。唯一的要求是主机应用程序还需要引用 Playwright-sharp 才能正确下载驱动程序。这是我可以忍受的。
当我尝试在 Docker (linux) 中运行我的 REST API 时出现问题。安装依赖项(libc6、libgdi 等)后,我得到了这个异常:
PlaywrightSharp.PlaywrightSharpException: Driver not found in any of the locations.
at PlaywrightSharp.Transport.Connection.GetExecutablePath() in /home/runner/work/playwright-sharp/playwright-sharp/src/PlaywrightSharp/Transport/Connection.cs:line 274
at PlaywrightSharp.Transport.Connection.GetProcess(String driverExecutablePath) in /home/runner/work/playwright-sharp/playwright-sharp/src/PlaywrightSharp/Transport/Connection.cs:line 233
at PlaywrightSharp.Transport.Connection.InstallAsync(String driverPath, String browsersPath) in /home/runner/work/playwright-sharp/playwright-sharp/src/PlaywrightSharp/Transport/Connection.cs:line 105
一个建议是从 Playwright-sharp 图像而不是 .NET 3.1 开始,但在我看来,这应该是一种包含所需文件并将它们放置在正确位置以使其工作的方法。我当前的 dockerfile 如下所示:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ./PlaywrightWrapper/nuget.config .
COPY ["PlaywrightWrapper/PlaywrightWrapper.csproj", "PlaywrightWrapper/"]
RUN dotnet restore "PlaywrightWrapper/PlaywrightWrapper.csproj" --configfile nuget.config
COPY . .
WORKDIR "/src/PlaywrightWrapper"
RUN dotnet build "PlaywrightWrapper.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "PlaywrightWrapper.csproj" -c Release -r linux-x64 -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# install depdendencies
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev \
&& rm -rf /var/lib/apt/lists/*
# copy the Playwright browsers from .NET build step
WORKDIR /root/.cache/ms-playwright
COPY --from=build /root/.cache/ms-playwright ./
WORKDIR /app
ENTRYPOINT ["dotnet", "PlaywrightWrapper.dll"]
这一步:
WORKDIR /root/.cache/ms-playwright
COPY --from=build /root/.cache/ms-playwright ./
将文件复制到与我在官方 Playwright-sharp docker 映像中看到的相同的文件夹中。
有没有人有一个工作的 Dockerfile 来安装所需的 Playwright 驱动程序,以便在 Docker 容器内生成无头 PDF?