From fc74d4361a6278dee17fc2ab4222a92d6de1096a Mon Sep 17 00:00:00 2001 From: martin legrand Date: Sat, 31 May 2025 19:31:25 +0200 Subject: [PATCH] latest attempt of dockerization --- .dockerignore | 5 ++--- Dockerfile.backend | 36 +++++++++--------------------------- sources/browser.py | 22 +++++++++------------- 3 files changed, 20 insertions(+), 43 deletions(-) diff --git a/.dockerignore b/.dockerignore index 14aab39..222b03b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,10 +3,9 @@ __pycache__/ *.py[cod] # Virtual environments -venv/ -.venv/ +agentic_seek_env/ +.agentic_seek_env/ -# Environment variables (secrets) .env # Git metadata diff --git a/Dockerfile.backend b/Dockerfile.backend index f5f51d2..821744c 100644 --- a/Dockerfile.backend +++ b/Dockerfile.backend @@ -2,7 +2,8 @@ FROM --platform=linux/amd64 python:3.11-slim ENV DEBIAN_FRONTEND=noninteractive -WORKDIR /app +RUN groupadd -r chrome && useradd -r -g chrome -G audio,video chrome + # Install essential packages and Chrome dependencies RUN apt-get update -y && apt-get install -y \ @@ -59,6 +60,8 @@ apt-get install -y \ ENV CHROME_TESTING_VERSION=134.0.6998.88 ENV DISPLAY=:99 +WORKDIR /app + RUN set -eux; \ wget -qO /tmp/chrome.zip \ "https://storage.googleapis.com/chrome-for-testing-public/${CHROME_TESTING_VERSION}/linux64/chrome-linux64.zip"; \ @@ -87,6 +90,7 @@ COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt RUN mkdir -p /opt/workspace +RUN mkdir -p /tmp && chmod 1777 /tmp # Copy application code COPY api.py . @@ -96,32 +100,10 @@ COPY crx/ crx/ COPY llm_router/ llm_router/ COPY config.ini . -# Install Chrome and ChromeDriver from chrome-for-testing -#RUN wget -O chrome-headless-shell-linux64.zip https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/linux64/chrome-headless-shell-linux64.zip && \ -# wget -O chromedriver-linux64.zip https://storage.googleapis.com/chrome-for-testing-public/137.0.7151.55/linux64/chromedriver-linux64.zip && \ -# unzip chrome-headless-shell-linux64.zip && \ -# unzip chromedriver-linux64.zip && \ -# mkdir -p /opt/google && \ -# ls -la && \ -# mv chrome-headless-shell-linux64 /opt/google/chrome && \ -# mv chromedriver-linux64/chromedriver /usr/local/bin/chromedriver && \ -# chmod +x /opt/google/chrome/chrome-headless-shell && \ -# chmod +x /usr/local/bin/chromedriver - -#RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - && \ -# echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list && \ -# apt-get update && \ -# apt-get install -y google-chrome-stable && \ -# rm -rf /var/lib/apt/lists/* -# -## Install matching ChromeDriver -#RUN CHROME_VERSION=$(google-chrome --version | grep -oP '\d+\.\d+\.\d+') && \ -# wget -O chromedriver.zip "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_VERSION%%.*}/chromedriver_linux64.zip" && \ -# unzip chromedriver.zip && \ -# mv chromedriver /usr/local/bin/ && \ -# chmod +x /usr/local/bin/chromedriver && \ -# rm chromedriver.zip - EXPOSE 8000 +# Switch to non-root user +USER chrome +RUN chown -R chrome:chrome /app +RUN chown -R chrome:chrome /tmp # Run the application CMD ["python3", "api.py"] \ No newline at end of file diff --git a/sources/browser.py b/sources/browser.py index 4f05f0d..64fd2a7 100644 --- a/sources/browser.py +++ b/sources/browser.py @@ -19,6 +19,7 @@ import time import random import os import shutil +import uuid import tempfile import markdownify import sys @@ -43,12 +44,11 @@ def get_chrome_path() -> str: "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta"] else: # Linux paths = ["/usr/bin/google-chrome", + "/opt/chrome/chrome", "/usr/bin/chromium-browser", "/usr/bin/chromium", - "/opt/chrome/chrome", "/usr/local/bin/chrome", "/opt/google/chrome/chrome-headless-shell", - "/opt/google/chrome/chrome", #"/app/chrome_bundle/chrome136/chrome-linux64" ] @@ -81,11 +81,6 @@ def install_chromedriver() -> str: Install the ChromeDriver if not already installed. Return the path. """ chromedriver_path = shutil.which("chromedriver") - #if not chromedriver_path: - # if os.path.exists("/app/chrome_bundle/chrome136/chromedriver"): - # print("Using bundled ChromeDriver from /app/chrome_bundle/chrome136/chromedriver") - # chromedriver_path = "/app/chrome_bundle/chrome136/chromedriver" - print("path:", chromedriver_path) if not chromedriver_path: try: print("ChromeDriver not found, attempting to install automatically...") @@ -136,27 +131,28 @@ def create_driver(headless=False, stealth_mode=True, crx_path="./crx/nopecha.crx if headless: #chrome_options.add_argument("--headless") chrome_options.add_argument("--headless=new") - chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--disable-webgl") - chrome_options.add_argument("--remote-debugging-port=9222") user_data_dir = tempfile.mkdtemp() user_agent = get_random_user_agent() width, height = (1920, 1080) - #chrome_options.add_argument(f"--user-data-dir={user_data_dir}") + user_data_dir = tempfile.mkdtemp(prefix="chrome_profile_") + import os + print(f"Running as UID: {os.getuid()}") # Will show 0 if root + chrome_options.add_argument("--no-sandbox") + chrome_options.add_argument('--disable-dev-shm-usage') + profile_dir = f"/tmp/chrome_profile_{uuid.uuid4().hex[:8]}" + chrome_options.add_argument(f'--user-data-dir={profile_dir}') chrome_options.add_argument(f"--accept-lang={lang}-{lang.upper()},{lang};q=0.9") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-background-timer-throttling") chrome_options.add_argument("--timezone=Europe/Paris") - chrome_options.add_argument("--no-sandbox") - chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--remote-debugging-port=9222') chrome_options.add_argument('--disable-background-timer-throttling') chrome_options.add_argument('--disable-backgrounding-occluded-windows') chrome_options.add_argument('--disable-renderer-backgrounding') chrome_options.add_argument('--disable-features=TranslateUI') chrome_options.add_argument('--disable-ipc-flooding-protection') - chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--mute-audio") chrome_options.add_argument("--disable-notifications") chrome_options.add_argument("--autoplay-policy=user-gesture-required")