PuppeteerSharp Docker 中运行报错解决方案
PuppeteerSharp Docker 中运行报错解决方案
前言
有一个.NET6的Webapi项目,使用到了PuppeteerSharp这个库。
但是在部署到Docker镜像中时遇到了一堆报错,下面总结下解决办法
1.启动慢问题
Puppeteer在第一次启动时会检测目录下是否有谷歌浏览器.local-chromium文件夹,如果没有则会自动到网上拉取。
拉取地址为:https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/884014/chrome-linux.zip
这个下载是比较耗时的,我自己的服务器大概要10分钟才能下载完成。如果下载超时nginx则会返回504 Gateway Time-out。
解决办法一、
增加nginx反向代理超时时间
主要就是下面两句配置
将反向代理读写超时设置为1000秒
proxy_read_timeout 1000;
proxy_connect_timeout 1000;
server {
listen 端口号;
server_name 您的域名;
proxy_read_timeout 1000;
proxy_connect_timeout 1000;
location / {
proxy_pass http://127.0.0.1:8222;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
解决办法二、
手动下载 https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/884014/chrome-linux.zip
并解压下载好的文件,构建如下目录结构
.local-chromium\Linux-884014\chrome-linux
最后将.local-chromium上传到Docker容器里
2.权限问题
An error occurred trying to start process '/app/.local-chromium/Linux-884014/chrome-linux/chrome' with working directory '/app'. Permission denied
在Docker容器中执行命令
chmod 777 /app/.local-chromium/Linux-884014/chrome-linux/chrome
3.各种依赖库不存在问题
在Docker容器中执行命令
apt-get install libnss3-dev -y && apt-get install libatk1.0-0 -y && apt-get install libatk-bridge2.0-0 libgtk-3.0 -y && apt-get install libgbm-dev -y && apt-get install libasound2 -y && apt-get install libglu1 -y && apt-get install libaio1 libaio-dev -y && apt-get install -y libgdiplus -y
4.Dockerfile全自动实现
首先需要先将.local-chromium.zip上传到cos上。
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 5000
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["DocApi.Web.Entry/DocApi.Web.Entry.csproj", "DocApi.Web.Entry/"]
COPY ["DocApi.Web.Core/DocApi.Web.Core.csproj", "DocApi.Web.Core/"]
COPY ["DocApi.StaticFile/DocApi.StaticFile.csproj", "DocApi.StaticFile/"]
COPY ["DocApi.Application/DocApi.Application.csproj", "DocApi.Application/"]
COPY ["DocApi.Core/DocApi.Core.csproj", "DocApi.Core/"]
COPY ["projects/html2openxml/src/Html2OpenXml/HtmlToOpenXml.csproj", "projects/html2openxml/src/Html2OpenXml/"]
RUN dotnet restore "DocApi.Web.Entry/DocApi.Web.Entry.csproj"
COPY . .
WORKDIR "/src/DocApi.Web.Entry"
RUN dotnet build "DocApi.Web.Entry.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DocApi.Web.Entry.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# 设置时区上海
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
# 清空apt源文件
RUN echo > /etc/apt/sources.list
# apt修改为国内源
RUN echo 'deb http://mirrors.ustc.edu.cn/debian buster main contrib non-free \ndeb http://mirrors.ustc.edu.cn/debian buster-backports main contrib non-free \ndeb http://mirrors.ustc.edu.cn/debian buster-proposed-updates main contrib non-free \ndeb http://mirrors.ustc.edu.cn/debian-security buster/updates main contrib non-free' >> /etc/apt/sources.list
# 清空apt缓存
RUN apt-get clean
RUN cat /etc/apt/sources.list
# 更新系统
RUN apt-get update
# 安装PuppeteerSharp依赖的库
RUN apt-get install libnss3-dev -y && apt-get install libatk1.0-0 -y && apt-get install libatk-bridge2.0-0 libgtk-3.0 -y && apt-get install libgbm-dev -y && apt-get install libasound2 -y && apt-get install libglu1 -y && apt-get install libaio1 libaio-dev -y && apt-get install -y libgdiplus -y && apt-get install unzip -y && apt-get install wget -y
# 从Cos下载
RUN wget -O .local-chromium.zip "https://您的cos文件地址"
# 解压缩
RUN unzip .local-chromium.zip
# 删除.local-chromium.zip
RUN rm -rf .local-chromium.zip
# 设置权限
RUN chmod 777 /app/.local-chromium/Linux-884014/chrome-linux/chrome
ENTRYPOINT ["dotnet", "DocApi.Web.Entry.dll"]