tfrere commited on
Commit
8e46fec
·
1 Parent(s): ca9a600
Files changed (2) hide show
  1. Dockerfile +17 -7
  2. app/server.py +22 -2
Dockerfile CHANGED
@@ -39,27 +39,37 @@ COPY pyproject.toml poetry.lock* ./
39
  RUN poetry config virtualenvs.create false \
40
  && poetry install --no-interaction --no-ansi --only main --no-root
41
 
42
- # Install Playwright browsers as root
43
- RUN playwright install chromium && \
44
- playwright install-deps chromium
45
-
46
  # Create directories and set permissions
47
  RUN mkdir -p static templates screenshots /home/user/.cache && \
48
  chown -R user:user /app /home/user/.cache
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # Copy application code
51
  COPY app /app/app
52
  COPY templates /app/templates
53
  COPY static /app/static
54
 
 
 
 
55
  # Make sure all files are owned by user
56
  RUN chown -R user:user /app
57
 
58
  # Environment variables
59
  ENV PORT=7860 \
60
- HOST=0.0.0.0 \
61
- PYTHONPATH=/app \
62
- HOME=/home/user
63
 
64
  # Switch to non-root user for running the app
65
  USER user
 
39
  RUN poetry config virtualenvs.create false \
40
  && poetry install --no-interaction --no-ansi --only main --no-root
41
 
 
 
 
 
42
  # Create directories and set permissions
43
  RUN mkdir -p static templates screenshots /home/user/.cache && \
44
  chown -R user:user /app /home/user/.cache
45
 
46
+ # Set HOME for the following Playwright install step
47
+ ENV HOME=/home/user \
48
+ PYTHONPATH=/app
49
+
50
+ # Switch to non-root user for browser installation
51
+ USER user
52
+
53
+ # Install Playwright browsers under the non-root user HOME directory
54
+ RUN playwright install chromium
55
+
56
+ # Switch back to root to copy files
57
+ USER root
58
+
59
  # Copy application code
60
  COPY app /app/app
61
  COPY templates /app/templates
62
  COPY static /app/static
63
 
64
+ # Install system dependencies for Playwright
65
+ RUN apt-get update && apt-get install -y fonts-noto-color-emoji fonts-freefont-ttf libharfbuzz-icu0
66
+
67
  # Make sure all files are owned by user
68
  RUN chown -R user:user /app
69
 
70
  # Environment variables
71
  ENV PORT=7860 \
72
+ HOST=0.0.0.0
 
 
73
 
74
  # Switch to non-root user for running the app
75
  USER user
app/server.py CHANGED
@@ -46,23 +46,43 @@ async def take_screenshot(url: str = Form(...)):
46
  filename = f"{uuid.uuid4()}.png"
47
  filepath = str(screenshots_path / filename)
48
 
 
 
 
 
49
  # Take the screenshot with Playwright
50
  async with async_playwright() as p:
51
  # Configuration adaptée pour l'environnement Docker
52
  browser = await p.chromium.launch(
53
  headless=True,
54
- args=['--no-sandbox', '--disable-dev-shm-usage']
 
 
 
 
 
 
 
 
 
 
55
  )
56
- page = await browser.new_page()
 
 
57
 
58
  try:
 
59
  await page.goto(url, wait_until="networkidle", timeout=60000)
 
60
  await page.screenshot(path=filepath)
 
61
  except Exception as e:
62
  logger.error(f"Error during page navigation or screenshot: {str(e)}")
63
  raise
64
  finally:
65
  await browser.close()
 
66
 
67
  return JSONResponse({
68
  "success": True,
 
46
  filename = f"{uuid.uuid4()}.png"
47
  filepath = str(screenshots_path / filename)
48
 
49
+ # Log browser executable paths
50
+ logger.info(f"HOME env: {os.environ.get('HOME')}")
51
+ logger.info(f"Current working directory: {os.getcwd()}")
52
+
53
  # Take the screenshot with Playwright
54
  async with async_playwright() as p:
55
  # Configuration adaptée pour l'environnement Docker
56
  browser = await p.chromium.launch(
57
  headless=True,
58
+ args=[
59
+ '--no-sandbox',
60
+ '--disable-setuid-sandbox',
61
+ '--disable-dev-shm-usage',
62
+ '--disable-accelerated-2d-canvas',
63
+ '--no-first-run',
64
+ '--no-zygote',
65
+ '--single-process',
66
+ '--disable-gpu'
67
+ ],
68
+ executable_path=None # Utiliser le chemin par défaut
69
  )
70
+
71
+ logger.info("Browser launched successfully")
72
+ page = await browser.new_page(viewport={"width": 1280, "height": 720})
73
 
74
  try:
75
+ logger.info(f"Navigating to URL: {url}")
76
  await page.goto(url, wait_until="networkidle", timeout=60000)
77
+ logger.info("Navigation complete, taking screenshot")
78
  await page.screenshot(path=filepath)
79
+ logger.info(f"Screenshot saved to {filepath}")
80
  except Exception as e:
81
  logger.error(f"Error during page navigation or screenshot: {str(e)}")
82
  raise
83
  finally:
84
  await browser.close()
85
+ logger.info("Browser closed")
86
 
87
  return JSONResponse({
88
  "success": True,