From 48f25566fa95b6f12e4edba1aa9f5bcc5f411679 Mon Sep 17 00:00:00 2001 From: Emilien Devos <4016501+unixfox@users.noreply.github.com> Date: Sat, 20 Jul 2024 10:42:31 +0200 Subject: [PATCH] builtin a x11 environment in docker --- Dockerfile | 21 ++++++++++++++++----- docker/scripts/startup.sh | 22 ++++++++++++++++++++++ index.py | 2 +- 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100755 docker/scripts/startup.sh diff --git a/Dockerfile b/Dockerfile index 54f4023..ae58b1f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,29 @@ FROM python:3.12-alpine - + +# Install dependencies RUN apk add --no-cache \ - chromium \ + xvfb \ + x11vnc \ + fluxbox \ nss \ freetype \ freetype-dev \ harfbuzz \ ca-certificates \ - ttf-freefont + ttf-freefont \ + chromium \ + chromium-chromedriver + +# Install x11vnc +RUN mkdir ~/.vnc +RUN x11vnc -storepasswd 1234 ~/.vnc/passwd WORKDIR /usr/app/src COPY index.py requirements.txt ./ -RUN pip install -r requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +COPY docker/scripts/startup.sh ./ # Run -CMD [ "python", "./index.py"] \ No newline at end of file +CMD [ "./startup.sh"] \ No newline at end of file diff --git a/docker/scripts/startup.sh b/docker/scripts/startup.sh new file mode 100755 index 0000000..501c9fa --- /dev/null +++ b/docker/scripts/startup.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +echo "internally launching GUI (X11 environment)" + +rm -f /tmp/.X0-lock + +# Run Xvfb on display 0. +Xvfb :0 -screen 0 1280x720x16 &>/dev/null & + +# Run fluxbox windows manager on display 0. +fluxbox -display :0 &>/dev/null & + +# Run x11vnc on display 0 +x11vnc -display :0 -forever -usepw &>/dev/null & + +# Add delay +sleep 5 + +echo "launching the python script" + +# Run python script on display 0 +DISPLAY=:0 python index.py diff --git a/index.py b/index.py index 987db10..546c2c1 100644 --- a/index.py +++ b/index.py @@ -6,7 +6,7 @@ import sys async def main(): tab_url = 'https://www.google.com/search?q=me+at+the+zoo&tbm=vid&source=lnms&hl=en&lr=lang_us' - browser = await start(headless=True) + browser = await start(headless=False) tab = browser.main_tab page = await browser.get(tab_url) accept_terms = await tab.find("Accept all")