tfrere commited on
Commit
9d5f27b
·
1 Parent(s): 01e5b7c
Files changed (2) hide show
  1. Dockerfile +37 -8
  2. app/server.py +14 -4
Dockerfile CHANGED
@@ -5,24 +5,52 @@ WORKDIR /app
5
  # Create non-root user
6
  RUN useradd -m -u 1000 user
7
 
8
- # Install system dependencies and Chrome for Playwright
9
  RUN apt-get update && apt-get install -y \
10
  wget \
11
  gnupg \
12
  curl \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  && pip install --upgrade pip \
14
  && pip install poetry
15
 
16
- # Copy poetry configuration and README
17
- COPY pyproject.toml poetry.lock* README.md* ./
18
 
19
  # Install Python dependencies using Poetry
20
  RUN poetry config virtualenvs.create false \
21
  && poetry install --no-interaction --no-ansi --only main --no-root
22
 
23
- # Install Playwright browsers
24
- RUN playwright install chromium && \
25
- playwright install-deps chromium
 
 
 
 
 
 
 
 
 
26
 
27
  # Create directories
28
  RUN mkdir -p static templates screenshots && \
@@ -36,9 +64,10 @@ COPY static /app/static
36
  # Environment variables
37
  ENV PORT=7860 \
38
  HOST=0.0.0.0 \
39
- PYTHONPATH=/app
 
40
 
41
- # Switch to non-root user
42
  USER user
43
 
44
  # Expose the port
 
5
  # Create non-root user
6
  RUN useradd -m -u 1000 user
7
 
8
+ # Install system dependencies
9
  RUN apt-get update && apt-get install -y \
10
  wget \
11
  gnupg \
12
  curl \
13
+ libnss3 \
14
+ libnspr4 \
15
+ libatk1.0-0 \
16
+ libatk-bridge2.0-0 \
17
+ libcups2 \
18
+ libdrm2 \
19
+ libdbus-1-3 \
20
+ libxkbcommon0 \
21
+ libx11-6 \
22
+ libxcomposite1 \
23
+ libxdamage1 \
24
+ libxext6 \
25
+ libxfixes3 \
26
+ libxrandr2 \
27
+ libgbm1 \
28
+ libpango-1.0-0 \
29
+ libcairo2 \
30
+ libasound2 \
31
+ libatspi2.0-0 \
32
  && pip install --upgrade pip \
33
  && pip install poetry
34
 
35
+ # Copy poetry configuration
36
+ COPY pyproject.toml poetry.lock* ./
37
 
38
  # Install Python dependencies using Poetry
39
  RUN poetry config virtualenvs.create false \
40
  && poetry install --no-interaction --no-ansi --only main --no-root
41
 
42
+ # Setup user permissions for Playwright cache
43
+ RUN mkdir -p /home/user/.cache && \
44
+ chown -R user:user /home/user
45
+
46
+ # Switch to non-root user for browser installation
47
+ USER user
48
+
49
+ # Install Playwright browsers correctly with the user that will run the app
50
+ RUN playwright install chromium --with-deps
51
+
52
+ # Switch back to root for directory creation
53
+ USER root
54
 
55
  # Create directories
56
  RUN mkdir -p static templates screenshots && \
 
64
  # Environment variables
65
  ENV PORT=7860 \
66
  HOST=0.0.0.0 \
67
+ PYTHONPATH=/app \
68
+ HOME=/home/user
69
 
70
+ # Switch to non-root user for running the app
71
  USER user
72
 
73
  # Expose the port
app/server.py CHANGED
@@ -48,11 +48,21 @@ async def take_screenshot(url: str = Form(...)):
48
 
49
  # Take the screenshot with Playwright
50
  async with async_playwright() as p:
51
- browser = await p.chromium.launch()
 
 
 
 
52
  page = await browser.new_page()
53
- await page.goto(url, wait_until="networkidle")
54
- await page.screenshot(path=filepath)
55
- await browser.close()
 
 
 
 
 
 
56
 
57
  return JSONResponse({
58
  "success": True,
 
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,