问题描述
我有一个运行在ASP.NET Core 3.1上的puppeteer-sharp实例,该实例在本地运行良好.为了进行部署,它内置在Docker容器中并部署到K8开发集群.docker容器构建良好,但应用程序在运行时失败,错误为 Failed to launch Base!/app/.local-chromium/Linux-706915/chrome-linux/chrome:加载共享库时出错:libX11.so.6:无法打开共享库文件:尝试使用操纵up时没有此类文件或目录
-锋利.
I have an instance of puppeteer-sharp running on ASP.NET Core 3.1 that works fine locally. For deployment, it gets built in a docker container and deployed to a K8 dev cluster. The docker container builds fine, but the app fails at runtime with the error Failed to launch Base! /app/.local-chromium/Linux-706915/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory
when trying to use puppeteer-sharp.
从以下资源中,我整理了以下dockerfile.我对Docker还是没有经验,所以可以想象配置有问题,但是我愿意接受建议!
From the following resources, I pieced together the below dockerfile. I'm still fairly inexperienced with docker so imagine it's something wrong with the config, but I'm open to suggestions!
使用的资源:
- http://www.hardkoded.com/blog/puppeteer-sharp-docker
- "> https://techoverflow.net/2018/06/05/how-to-fix-puppetteer-error-加载共享库libx11-xcb-so-1-时无法打开共享对象文件没有这样的文件或目录/
- Docker NodeJS Puppeteer@ 2.0.0-如何修复无法启动Chrome!问题(无头=假无效)
- http://www.hardkoded.com/blog/puppeteer-sharp-docker
- https://techoverflow.net/2018/06/05/how-to-fix-puppetteer-error-while-loading-shared-libraries-libx11-xcb-so-1-cannot-open-shared-object-file-no-such-file-or-directory/
- Docker NodeJS [email protected] - How do fix Failed to launch chrome! issue (headless = false had no effect)
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
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
#####################
#PUPPETEER RECIPE
#####################
# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
ARG CHROME_VERSION="81.0.4044.138-1"
RUN apt-get update && apt-get -f install && apt-get -y install apt-utils wget gnupg2
RUN apt-get -f install && apt-get -y install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
RUN wget --no-verbose -O /tmp/chrome.deb http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
&& apt-get update \
&& apt-get install -y /tmp/chrome.deb --no-install-recommends --allow-downgrades \
&& rm /tmp/chrome.deb
# NOTE: REMOVED THIS WHILST DEBUGGING - D
# Add user, so we don't need --no-sandbox.
# same layer as npm install to keep re-chowned files from using up several hundred MBs more space
#RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
#&& mkdir -p /home/pptruser/Downloads \
#&& chown -R pptruser:pptruser /home/pptruser
#
## Run everything after as non-privileged user.
#
#USER pptruser
ENV PUPPETEER_EXECUTABLE_PATH "/usr/bin/google-chrome-unstable"
#####################
#END PUPPETEER RECIPE
#####################
COPY ["ProjectName.Core/ProjectName.Core.csproj", "ProjectName.Core/"]
RUN dotnet restore "ProjectName.Core/ProjectName.Core.csproj"
COPY . .
WORKDIR "/src/ProjectName.Core"
RUN dotnet build "ProjectName.Core.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ProjectName.Core.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.Core.dll"]
代码实现:
public static async Task<Stream> GeneratePdfFromHtml(string html, List<string> styleUrls = null)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});
using (var page = await browser.NewPageAsync())
{
await page.SetContentAsync(html);
if(styleUrls != null)
foreach (var url in styleUrls)
{
await page.AddStyleTagAsync(url);
}
return await page.PdfStreamAsync();
}
}
使用Docker的资源都以.NET Core 2.0为目标,所以我不知道这是否与它有关.它可以在Docker环境之外正常运行,这说明.NET Core版本不是问题,更可能是Dockerfile本身迈出的一步.
The resources using Docker all target .NET Core 2.0 so I don't know if that has anything to do with it. The fact that it runs fine outside of a Docker environment suggests the .NET Core version isn't the problem and is more likely a step in the dockerfile itself.
推荐答案
好吧,事实证明这很复杂.dockerfile需要重新排列,并且在没有沙箱的情况下效果最佳.如您所见,原始代码是在中间映像而不是最终映像/基础上安装依赖项.
Ok, turns out it was a mix of things. The dockerfile needed rearranging and works best without a sandbox. As you'll see, the original code was installing dependencies on the intermediate image rather then the final/base.
由于某种原因,我无法使其在沙盒用户下正常工作,因此将其删除.结果,该实现需要更新以启用--no-sandbox参数.
For some reason, I couldn't get it to work under a sandboxed user properly, so that was removed. As a result the implementation needed updating to enable the --no-sandbox argument.
这是所有努力使puppeteer-sharp与.NET Core 3.1配合使用的其他人的更新代码.
Here's the updated code for anyone else struggling to get puppeteer-sharp working with .NET Core 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
#####################
#PUPPETEER RECIPE
#####################
# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
ARG CHROME_VERSION="81.0.4044.138-1"
RUN apt-get update && apt-get -f install && apt-get -y install wget gnupg2 apt-utils
RUN wget --no-verbose -O /tmp/chrome.deb http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
&& apt-get update \
&& apt-get install -y /tmp/chrome.deb --no-install-recommends --allow-downgrades fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf \
&& rm /tmp/chrome.deb
ENV PUPPETEER_EXECUTABLE_PATH "/usr/bin/google-chrome"
#####################
#END PUPPETEER RECIPE
#####################
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ProjectName.Core/ProjectName.Core.csproj", "ProjectName.Core/"]
RUN dotnet restore "ProjectName.Core/ProjectName.Core.csproj"
COPY . .
WORKDIR "/src/ProjectName.Core"
RUN dotnet build "ProjectName.Core.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ProjectName.Core.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.Core.dll"]
实施
public static async Task<Stream> GeneratePdfFromHtml(string html, List<string> styleUrls = null)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Args = new[] { "--no-sandbox" },
Headless = true
});
using (var page = await browser.NewPageAsync())
{
await page.SetContentAsync(html);
if(styleUrls != null)
foreach (var url in styleUrls)
{
await page.AddStyleTagAsync(new AddTagOptions { Path = url });
}
return await page.PdfStreamAsync();
}
}
可能值得注意的是,使用本地URL(例如localhost:1234/lib/bootsrap/bootsrap.min.css)查找样式表时,puppeteer-sharp的docker实现似乎也存在问题.它只是永久悬挂和旋转,因此将 AddStyleTagAsync()
更改为使用文件路径.别忘了,它位于IHostingEnvironment.WebRootPath下,可以为您保存Google搜索.
It's probably worth noting that the docker implementation of puppeteer-sharp also seems to have a problem with finding stylesheets using a local url (e.g. localhost:1234/lib/bootsrap/bootsrap.min.css). It just hangs and spins forever, hence changing the AddStyleTagAsync()
to use filepaths instead. Don't forget, this is found under IHostingEnvironment.WebRootPath to save you a google search.
希望这可以帮助其他人.毫无疑问,它也将在不久的将来再次跨越我的道路!
Hope this helps someone else out there. No doubt it'll cross my path again at some point in the not-so-distant future too!
这篇关于无法启动基地!/app/.local-chromium/Linux-706915/chrome-linux/chrome:... libX11.so.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!