make patchright work in docker

This commit is contained in:
Ralf Vogler 2025-05-26 00:46:09 +02:00
parent 6ce17f667e
commit 34b24f3eb7
3 changed files with 32 additions and 47 deletions

View file

@ -1,5 +1,5 @@
# FROM mcr.microsoft.com/playwright:v1.20.0 # FROM mcr.microsoft.com/playwright:v1.20.0
# Partially from https://github.com/microsoft/playwright/blob/main/utils/docker/Dockerfile.focal # Partially from https://github.com/microsoft/playwright/blob/main/utils/docker/Dockerfile.jammy
FROM ubuntu:jammy FROM ubuntu:jammy
# Configuration variables are at the end! # Configuration variables are at the end!
@ -8,9 +8,9 @@ FROM ubuntu:jammy
SHELL ["/bin/bash", "-o", "pipefail", "-c"] SHELL ["/bin/bash", "-o", "pipefail", "-c"]
ARG DEBIAN_FRONTEND=noninteractive ARG DEBIAN_FRONTEND=noninteractive
# Install up-to-date node & npm, deps for virtual screen & noVNC, firefox, pip for apprise. # Install nodejs and deps for virtual display, noVNC, chromium, and pip for installing apprise.
RUN apt-get update \ RUN apt-get update \
&& apt-get install --no-install-recommends -y curl ca-certificates gnupg \ && apt-get install -y --no-install-recommends curl ca-certificates gnupg \
&& mkdir -p /etc/apt/keyrings \ && mkdir -p /etc/apt/keyrings \
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
@ -23,43 +23,38 @@ RUN apt-get update \
novnc websockify \ novnc websockify \
dos2unix \ dos2unix \
python3-pip \ python3-pip \
# && npx playwright install-deps firefox \ # RUN npx patchright install-deps chromium
&& apt-get install --no-install-recommends -y \ # ^ installing deps manually instead saved ~130MB:
libgtk-3-0 \ && apt-get install -y --no-install-recommends \
libasound2 \ libnss3 \
libxcomposite1 \ libnspr4 \
libpangocairo-1.0-0 \
libpango-1.0-0 \
libatk1.0-0 \ libatk1.0-0 \
libcairo-gobject2 \ libatk-bridge2.0-0 \
libcups2 \
libxkbcommon0 \
libatspi2.0-0 \
libxcomposite1 \
libgbm1 \
libpango-1.0-0 \
libcairo2 \ libcairo2 \
libgdk-pixbuf-2.0-0 \ libasound2 \
libdbus-glib-1-2 \
libxcursor1 \
&& apt-get autoremove -y \ && apt-get autoremove -y \
# https://www.perplexity.ai/search/what-files-do-i-need-to-remove-imjwdphNSUWK98WzsmQswA
&& apt-get clean \ && apt-get clean \
&& rm -rf \ && rm -rf \
/var/lib/apt/lists/* \
/var/cache/* \
/var/tmp/* \
/tmp/* \ /tmp/* \
/usr/share/doc/* \ /usr/share/doc/* \
/var/cache/* \ && ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html \
/var/lib/apt/lists/* \ && pip install --no-cache-dir apprise
/var/tmp/*
# RUN node --version
# RUN npm --version
RUN ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html
RUN pip install --no-cache-dir apprise
WORKDIR /fgc WORKDIR /fgc
COPY package*.json ./ COPY package*.json ./
# Playwright installs patched firefox to ~/.cache/ms-playwright/firefox-* # --no-shell to avoid installing chromium_headless_shell (307MB) since headless mode could be detected without patching the browser itself
# Requires some system deps to run (see inlined install-deps above). RUN npm install && npx patchright install chromium --no-shell && du -h -d1 ~/.cache/ms-playwright
RUN npm install
# Old: PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD + install firefox (had to be done after `npm install` to get the correct version). Now: playwright-firefox as npm dep and `npm install` will only install that.
# From 1.38 Playwright will no longer install browser automatically for playwright, but apparently still for playwright-firefox: https://github.com/microsoft/playwright/releases/tag/v1.38.0
# RUN npx playwright install firefox
COPY . . COPY . .

View file

@ -6,28 +6,16 @@ echo "Version: https://github.com/vogler/free-games-claimer/tree/${COMMIT}"
[ -n "$BRANCH" ] && [ "$BRANCH" != "main" ] && echo "Branch: ${BRANCH}" [ -n "$BRANCH" ] && [ "$BRANCH" != "main" ] && echo "Branch: ${BRANCH}"
echo "Build: $NOW" echo "Build: $NOW"
BROWSER="${BROWSER_DIR:-data/browser}"
# Remove chromium profile lock. # Remove chromium profile lock.
# When running in docker and then killing it, on the next run chromium displayed a dialog to unlock the profile which made the script time out. # When running in docker and then killing it, on the next run chromium displayed a dialog to unlock the profile which made the script time out.
# Maybe due to changed hostname of container or due to how the docker container kills playwright - didn't check. # Maybe due to changed hostname of container or due to how the docker container kills playwright - didn't check.
# https://bugs.chromium.org/p/chromium/issues/detail?id=367048 # https://bugs.chromium.org/p/chromium/issues/detail?id=367048
rm -f /fgc/data/browser/SingletonLock rm -f /fgc/$BROWSER/SingletonLock
# Firefox preferences are stored in $BROWSER_DIR/pref.js and can be overridden by a file user.js
# Since this file has to be in the volume (data/browser), we can't do this in Dockerfile.
mkdir -p /fgc/data/browser
# fix for 'Incorrect response' after solving a captcha correctly - https://github.com/vogler/free-games-claimer/issues/261#issuecomment-1868385830
# echo 'user_pref("privacy.resistFingerprinting", true);' > /fgc/data/browser/user.js
cat <<EOT >/fgc/data/browser/user.js
user_pref("privacy.resistFingerprinting", true);
// user_pref("privacy.resistFingerprinting.letterboxing", true);
// user_pref("browser.contentblocking.category", "strict");
// user_pref("webgl.disabled", true);
EOT
# TODO disable session restore message?
# Remove X server display lock, fix for `docker compose up` which reuses container which made it fail after initial run, https://github.com/vogler/free-games-claimer/issues/31 # Remove X server display lock, fix for `docker compose up` which reuses container which made it fail after initial run, https://github.com/vogler/free-games-claimer/issues/31
# echo $DISPLAY # Maybe no longer needed after adding #478's -nolisten unix below
# ls -l /tmp/.X11-unix/
rm -f /tmp/.X1-lock rm -f /tmp/.X1-lock
# 6000+SERVERNUM is the TCP port Xvfb is listening on: # 6000+SERVERNUM is the TCP port Xvfb is listening on:
@ -36,10 +24,11 @@ rm -f /tmp/.X1-lock
# Options passed directly to the Xvfb server: # Options passed directly to the Xvfb server:
# -ac disables host-based access control mechanisms # -ac disables host-based access control mechanisms
# screen NUM WxHxD creates the screen and sets its width, height, and depth # screen NUM WxHxD creates the screen and sets its width, height, and depth
# -nolisten unix tells the server not to use Unix domain sockets, thus avoiding the need to create /tmp/.X11-unix
export DISPLAY=:1 # need to export this, otherwise playwright complains with 'Looks like you launched a headed browser without having a XServer running.' export DISPLAY=:1 # need to export this, otherwise playwright complains with 'Looks like you launched a headed browser without having a XServer running.'
Xvfb $DISPLAY -ac -screen 0 "${WIDTH}x${HEIGHT}x${DEPTH}" & Xvfb $DISPLAY -ac -screen 0 "${WIDTH}x${HEIGHT}x${DEPTH}" &
echo "Xvfb display server created screen with resolution ${WIDTH}x${HEIGHT}" echo "Xvfb display server created screen with resolution ${WIDTH}x${HEIGHT} -nolisten unix"
if [ -z "$VNC_PASSWORD" ]; then if [ -z "$VNC_PASSWORD" ]; then
pw="-nopw" pw="-nopw"
pwt="no password!" pwt="no password!"

View file

@ -31,7 +31,8 @@ const context = await chromium.launchPersistentContext(cfg.dir.browser, {
args: [ args: [
'--hide-crash-restore-bubble', '--hide-crash-restore-bubble',
], ],
chromiumSandbox: true, // https://github.com/Kaliiiiiiiiii-Vinyzu/patchright/issues/52 // The following makes the browser crash in docker with 'Chromium sandboxing failed!':
// chromiumSandbox: true, // https://github.com/Kaliiiiiiiiii-Vinyzu/patchright/issues/52
}); });
// console.log(context.browser().browserType()); // browser is null... // console.log(context.browser().browserType()); // browser is null...