node.js - 无法使用 Puppeteer 运行 Bazel nodejs_image(错误:libgobject-2.0.so.0)
问题描述
我正在使用 Bazel 构建 Docker 容器:
ts_config(
name = "tsconfig",
src = "tsconfig.lib.json",
)
ts_project(
name = "lib",
srcs = ["index.ts"],
declaration = True,
tsconfig = "tsconfig",
deps = [
"@npm//@types/node",
"@npm//puppeteer",
],
)
nodejs_binary(
name = "server",
data = [
"lib",
],
entry_point = "index.ts",
)
nodejs_image(
name = "image",
binary = "server",
)
运行nodejs_binary
正常。
但是运行nodejs_image
“图像”会引发错误:
(node:44) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
/app/server.runfiles/node_puppeteer/node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
因此,我尝试添加一个自定义base
(这个)图像,如下所示:
nodejs_image(
name = "base_image",
base = "@nodejs_puppeteer//image",
binary = "server",
)
并在WORKSPACE
:
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
container_pull(
name = "nodejs_puppeteer",
digest = "sha256:22ec485fa257ec892efc2a8b69ef9a3a2a81a0f6622969ffe2d416d2a076214b",
registry = "docker.io",
repository = "drakery/node-puppeteer:latest",
)
但是运行更新的nodejs_image
“base_image”会引发此错误:
[link_node_modules.js] An error has been reported: [Error: EACCES: permission denied, symlink '/app/server.runfiles/npm/node_modules' -> 'node_modules'] {
errno: -13,
code: 'EACCES',
syscall: 'symlink',
path: '/app/server.runfiles/npm/node_modules',
dest: 'node_modules'
} Error: EACCES: permission denied, symlink '/app/server.runfiles/npm/node_modules' -> 'node_modules'
如何将缺少的依赖项添加到nodejs_image
?
可以在此处找到该问题的最小再现:https ://github.com/flolu/bazel-node-puppeteer
解决方案
正如@Rohan Singh和@Noam Yizraeli所建议的那样,我尝试将自定义基础映像更改为我的开发环境。因此,我使用 Ubuntu 创建了一个 Docker 映像,它是基本的,并且安装了 Node.js 和 Chrome:
FROM ubuntu:20.04
# Install Node.js
RUN apt-get update \
&& apt-get install -y curl
RUN curl --silent --location https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install --yes nodejs
RUN apt-get install --yes build-essential
# Install Chrome
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
我把它拉到巴泽尔是这样的:
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
container_pull(
name = "ubuntu",
digest = "sha256:a1ceb3aac586b6377821ffe6aede35c3646649ee5ac38c3566799cd04745257f",
registry = "docker.io",
repository = "drakery/node-puppeteer",
)
并像这样使用它:
nodejs_image(
name = "custom_ubuntu",
base = "@ubuntu//image",
binary = "server",
)
这是最终的工作存储库:https ://github.com/flolu/bazel-node-puppeteer/tree/050376d36bccb67a93933882a459f0af3051eabd