docker 容器中托管的 .NET Core 3.1 应用中的 Playwright-sharp
Playwright-sharp in .NET Core 3.1 app hosted in docker container
我创建了一个 .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 开始,但在我看来,这应该是一种包含所需文件并将它们放置在正确位置以使其工作的方法。我当前的 docker 文件如下所示:
#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 图像中看到的同一文件夹。
有没有人有工作的 Docker 文件,可以在 Docker 容器中安装无头 PDF 生成所需的 Playwright 驱动程序?
终于有时间再研究一下,想出了解决办法。
一些使用 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
我创建了一个 .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 开始,但在我看来,这应该是一种包含所需文件并将它们放置在正确位置以使其工作的方法。我当前的 docker 文件如下所示:
#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 图像中看到的同一文件夹。
有没有人有工作的 Docker 文件,可以在 Docker 容器中安装无头 PDF 生成所需的 Playwright 驱动程序?
终于有时间再研究一下,想出了解决办法。
一些使用 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