Skip to content

Commit

Permalink
build(docker): put bundled chrome in node_modules (DIYgod#12075)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Mar 10, 2023
1 parent 04cb371 commit 98a0373
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.devcontainer
.github
.idea
.vscode
Expand All @@ -16,11 +17,13 @@ test
.env
.eslint*
.gitignore
.gitpod.yml
.prettier*
.(yarn|npm|nvm)rc
*.md
app.json
docker-compose*
fly.toml
jsconfig.json
npm-debug.log
process.json
Expand All @@ -33,3 +36,7 @@ vercel.json
.git/index
.git/info
.git/hooks

#rsshub auxiliary files
lib/radar-rules.js
lib/v2/**/radar.js
9 changes: 9 additions & 0 deletions .puppeteerrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { join } = require('path');

/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, 'node_modules', '.cache', 'puppeteer'),
};
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ FROM node:16-bullseye-slim as chromium-downloader
# Yeah, downloading Chromium never needs those dependencies below.

WORKDIR /app
COPY ./.puppeteerrc.js /app/
COPY --from=dep-version-parser /ver/.puppeteer_version /app/.puppeteer_version

ARG TARGETPLATFORM
ARG USE_CHINA_NPM_REGISTRY=0
ARG PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1
# The official recommended way to use Puppeteer on x86(_64) is to use the bundled Chromium from Puppeteer:
# https://github.com/puppeteer/puppeteer#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy
# https://pptr.dev/faq#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy
RUN \
set -ex ; \
if [ "$PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" = 0 ] && [ "$TARGETPLATFORM" = 'linux/amd64' ]; then \
Expand All @@ -98,7 +99,7 @@ RUN \
yarn add puppeteer@$(cat /app/.puppeteer_version) && \
yarn cache clean ; \
else \
mkdir -p /root/.cache/puppeteer ; \
mkdir -p /app/node_modules/.cache/puppeteer ; \
fi;

# ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -115,11 +116,11 @@ WORKDIR /app
# install deps first to avoid cache miss or disturbing buildkit to build concurrently
ARG TARGETPLATFORM
ARG PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1
# https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
# https://pptr.dev/troubleshooting#chrome-headless-doesnt-launch-on-unix
# https://github.com/puppeteer/puppeteer/issues/7822
# https://www.debian.org/releases/bullseye/amd64/release-notes/ch-information.en.html#noteworthy-obsolete-packages
# The official recommended way to use Puppeteer on arm/arm64 is to install Chromium from the distribution repositories:
# https://github.com/puppeteer/puppeteer/blob/94cb08c85955c0688d12b6ed10e61a4581a01280/src/node/BrowserFetcher.ts#L116-L119
# https://github.com/puppeteer/puppeteer/blob/07391bbf5feaf85c191e1aa8aa78138dce84008d/packages/puppeteer-core/src/node/BrowserFetcher.ts#L128-L131
RUN \
set -ex && \
apt-get update && \
Expand All @@ -143,14 +144,14 @@ RUN \
fi; \
rm -rf /var/lib/apt/lists/*

COPY --from=chromium-downloader /root/.cache/puppeteer /root/.cache/puppeteer
COPY --from=chromium-downloader /app/node_modules/.cache/puppeteer /app/node_modules/.cache/puppeteer

# if grep matches nothing then it will exit with 1, thus, we cannot `set -e` here
RUN \
set -x && \
if [ "$PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" = 0 ] && [ "$TARGETPLATFORM" = 'linux/amd64' ]; then \
echo 'Verifying Chromium installation...' && \
ldd $(find /root/.cache/puppeteer/ -name chrome) | grep "not found" ; \
ldd $(find /app/node_modules/.cache/puppeteer/ -name chrome -type f) | grep "not found" ; \
if [ "$?" = 0 ]; then \
echo "!!! Chromium has unmet shared libs !!!" && \
exit 1 ; \
Expand All @@ -177,7 +178,7 @@ CMD ["npm", "run", "start"]
# apt-file \
# && \
# apt-file update && \
# ldd $(find /app/node_modules/puppeteer/ -name chrome) | grep -Po "\S+(?= => not found)" | \
# ldd $(find /app/node_modules/.cache/puppeteer/ -name chrome -type f) | grep -Po "\S+(?= => not found)" | \
# sed 's/\./\\./g' | awk '{print $1"$"}' | apt-file search -xlf - | grep ^lib | \
# xargs -d '\n' -- \
# apt-get install -yq --no-install-recommends \
Expand Down

0 comments on commit 98a0373

Please sign in to comment.