Иван Гречка
Иван Гречка

Reputation: 153

Docker and playwright

I need to install playwright inside of docker. This is my dockerfile.

FROM python:3.9

EXPOSE 8000

WORKDIR /fastanalytics

COPY /requirements.txt /fastanalytics/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /fastanalytics/requirements.txt

RUN playwright install
RUN playwright install-deps
RUN apt-get update && apt-get upgrade -y

But when I am installing I got the below error. I tried installing everything in the error message but it didn't help.

E: Package 'ttf-ubuntu-font-family' has no installation candidate
E: Unable to locate package libenchant1c2a
E: Unable to locate package libicu66
E: Package 'libjpeg-turbo8' has no installation candidate

enter image description here

Upvotes: 14

Views: 12932

Answers (5)

Anand siva
Anand siva

Reputation: 11

Following Dockerfile is working, compiled from other answers

# Use the official Python 3.11 image as the base
FROM python:3.11

# Set the working directory in the container
WORKDIR /app

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright

# Install system dependencies
RUN apt-get update && apt-get install -y

# Install Playwright
RUN pip install playwright

# Install Playwright browsers and dependencies
RUN playwright install --with-deps chromium

# Create a simple Python script to test Playwright with example.com
RUN echo "from playwright.sync_api import sync_playwright\n\
def run(playwright):\n\
    browser = playwright.chromium.launch()\n\
    page = browser.new_page()\n\
    page.goto('https://example.com')\n\
    print(page.title())\n\
    browser.close()\n\
\n\
with sync_playwright() as playwright:\n\
    run(playwright)" > test_example.py

# Command to run the Playwright test script
CMD ["python", "test_example.py"]

Upvotes: 1

Kid Yume
Kid Yume

Reputation: 157

There has been a fix to this and in order to get this working properly you just have to install the actual chromium binary files to a specific directory during install and then specify that directory as a Environment variable.

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10.7

ENV PYTHONDONTWRITEBYTECODE=1
ENV PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
#..... whatever else you need 
RUN pip install playwright
# install manually all the missing libraries
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
RUN PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright python -m playwright install --with-deps chromium

If it errors out try to throw a

RUN sudo apt-get clean
RUN sudo apt-get update

before it

Upvotes: 4

wanna_coder101
wanna_coder101

Reputation: 206

Microsoft released a python docker image for Playwright

Dockerfile

# Build Environment: Playwright
FROM mcr.microsoft.com/playwright/python:v1.21.0-focal

# Add python script to Docker
COPY index.py /

# Run Python script
CMD [ "python", "index.py" ]

Check Playwright - Docker docs for the latest playwright version.

Upvotes: 18

MrDiggles
MrDiggles

Reputation: 768

Based on their docs (https://playwright.dev/python/docs/cli#install-system-dependencies), it looks like they only officially support Ubuntu systems whereas the python:3.9 is based off of Debian. Try using Ubuntu as your base image and install python on it:

FROM ubuntu:20.04

RUN apt update
RUN apt install python3.9

...

Upvotes: 6

Ali Samji
Ali Samji

Reputation: 479

RUN apt-get update && playwright install-deps

While I have not used playwright, this seems to be a Docker issue. Playwright seems to be using apt-get to install the dependencies. In that case, you need to ensure apt-get update is run before playwright install-deps. While you can use two separate RUN statements to accomplish this, it is not advisable sinceapt-get update may not be run because of Docker's caching capabilities.

Upvotes: 1

Related Questions