我创建了一个.NET Core 3.1 nuget库,该库围绕Playwright sharp v0.192.0封装了功能。我在一个RESTneneneba API(也是.NETCore3.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,可以在Docker容器中安装无头PDF生成所需的Playwright驱动程序?
终于有时间再次研究这个问题,并提出了解决方案。
使用剧作家敏锐的码头工人形象(此处建议(的几种有前景的方法在我的情况下都不起作用。仍然不确定确切的原因,但我不得不手动添加所有依赖项。只需确保将剧作家浏览器版本(npm i..(与您正在使用的playwright Sharp NuGet包相匹配(在我的情况下为0.192.0(:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
RUN apt-get update && apt-get install -y --no-install-recommends
xvfb
RUN apt-get update && apt-get install -y --no-install-recommends
fonts-liberation
libasound2
libatk-bridge2.0-0
libatk1.0-0
libatspi2.0-0
libcairo2
libcups2
libdbus-1-3
libdrm2
libgbm1
libglib2.0-0
libgtk-3-0
libnspr4
libnss3
libpango-1.0-0
libx11-6
libxcb1
libxcomposite1
libxdamage1
libxext6
libxfixes3
libxrandr2
RUN apt-get update && apt-get install -y npm &&
npm i playwright-chromium@1.9.2 &&
npm i playwright-firefox@1.9.2 &&
npm i playwright-webkit@1.9.2
RUN apt-get remove nodejs -y
... the rest of your docker file here