在Google Cloud Run/Cloud Build中让Puppeteer工作的关键是配置正确的环境和依赖项。下面是一些步骤和建议:
# 使用Node.js 14作为基础镜像
FROM node:14
# 安装Chromium依赖项
RUN apt-get update && apt-get install -y \
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 \
wget
# 创建app目录并将应用程序代码复制到镜像中
WORKDIR /app
COPY package.json .
COPY package-lock.json .
RUN npm install
# 将应用程序代码复制到镜像中
COPY . .
# 设置环境变量
ENV PORT=8080
# 启动应用程序
CMD [ "npm", "start" ]
"dependencies": {
"puppeteer": "^10.4.0"
}
steps:
# 构建Docker镜像
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/your-app', '.']
# 推送Docker镜像到Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/your-app']
# 部署到Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'your-app', '--image', 'gcr.io/$PROJECT_ID/your-app', '--platform', 'managed', '--region', 'us-central1']
gcloud builds submit --config cloudbuild.yaml .
领取专属 10元无门槛券
手把手带您无忧上云