如何解决托管在 docker 容器中的 .NET Core 3.1 应用程序中的剧作家
我创建了一个 .NET Core 3.1 nuget 库,该库包含 Playwright-sharp v0.192.0 的功能。我在 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?
解决方法
终于有时间再次研究这个问题,并提出了一个解决方案。
使用 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 here
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。