我已经创建了一个.NET Core3.1Nuget库,它围绕Playwright-sharp v0.192.0包装了功能。我在REST API (也是.NET Core3.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驱动程序?
发布于 2021-06-03 09:25:12
终于有时间再次研究这个问题,并提出了一个解决方案。
在我的例子中,使用playwrightsharp docker图像(建议使用here)的几种有希望的方法都不起作用。我仍然不确定确切的原因,但我不得不手动添加所有依赖项。只要确保与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 herehttps://stackoverflow.com/questions/67204982
复制相似问题