Playwright con Docker su Synology DS920+

Il mio Synology DS920+, grazie a Docker, fra le tante attività, può schedulare ed eseguire script Playwright. Può essere un web o mail server, un repository Subversion o Git, un server Emby o Plex. Può servire come download station e svolgere una miriade di altre attività utili…

Synology DS920+
Synology DS920+

Di seguito, un dockerfile che partendo da Ubuntu 20.04 LTS mette a disposizione Playwright per Python, pronto per l’esecuzione di script. E’ stato pensato per essere usato (anche) su NAS Synology.
Si può scegliere se inserire nell’immagine uno solo dei tre browser predefiniti (Webkit, Firefox, Chromium), limitando le dimensioni tra i 550Mb/750Mb.

#
# Compile examples:
#		- Generic build (larger)
# 		docker image build -t ubuntu:python-playwright -f Dockerfile .
#       
#		- Install only chromium browser, with my Synology docker user's GROUP_ID/USER_ID (lighter)
#		docker image build --build-arg BROWSER=chromium --build-arg GROUP_ID=65000 --build-arg USER_ID=1000 -t ubuntu:python-playwright -f Dockerfile .
#
# Run example:
#		docker run --rm --mount type=bind,source="$(pwd)",target=/home/play/work ubuntu:python-playwright python work/windtre.py
#

FROM ubuntu:focal

# If empty install all default browsers
ARG BROWSER

# Python additional packages
ARG PACKAGES

# DOCKER UID, GID passed as arg
ARG USER_ID
ARG GROUP_ID

# Predefined args
ARG TZ='Europe/Rome'
ARG LOCALE=it_IT.UTF-8
ARG USER=play
ARG HOME_DIR=/home/${USER}
ARG DEBIAN_FRONTEND=noninteractive 

# LABEL about the custom image
LABEL maintainer="Francesco Guarnieri"
LABEL version="0.3"
LABEL description="Ubuntu 20.04LTS docker image for Python and Playwright"

# Install WebKit dependencies
RUN apt-get update && \
	apt-get install locales && \
	# Localization
	locale-gen --no-purge ${LOCALE} && \
	apt-get install -y --no-install-recommends \
	libwoff1 \
	libopus0 \
	libwebp6 \
	libwebpdemux2 \
	libenchant1c2a \
	libgudev-1.0-0 \
	libsecret-1-0 \
	libhyphen0 \
	libgdk-pixbuf2.0-0 \
	libegl1 \
	libnotify4 \
	libxslt1.1 \
	libevent-2.1-7 \
	libgles2 \
	libxcomposite1 \
	libatk1.0-0 \
	libatk-bridge2.0-0 \
	libepoxy0 \
	libgtk-3-0 \
	libharfbuzz-icu0 \
	# Install latest Python
	python3 \ 
	python3-pip && \
	if test -z "${BROWSER}" -o "${BROWSER}" = "webkit" ; then \
	apt-get install -y --no-install-recommends \
	libgl1 \
	libvpx6 \
	gstreamer1.0-plugins-base \
	libgstreamer1.0-0 \
	libgstreamer-gl1.0-0 \
	gstreamer1.0-plugins-bad \
	libopenjp2-7 \
	gstreamer1.0-libav \
	; fi && \
	if test -z "${BROWSER}" -o "${BROWSER}" = "chromium" ; then \
	apt-get install -y --no-install-recommends \
	libnss3 \
	libnspr4 \
	libasound2 \
	; fi && \
	if test -z "${BROWSER}" -o "${BROWSER}" = "firefox" ; then \
	apt-get install -y --no-install-recommends \
	libdbus-glib-1-2 \
	libxt6 \
	; fi && \
# (Optional) Install XVFB if there's a need to run browsers in headful mode
#	xvfb  \    
# Clean \
	apt-get -y autoclean && \
	apt-get -y autoremove && \
	rm -rf /var/lib/apt/lists/* && \
# Alternatives
	update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1 && \
	update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \
# Use GID and UID arg, if none passed are used default values
	if getent group ${GROUP_ID}>/dev/null; then groupadd ${USER}; \
	else groupadd -g ${GROUP_ID} ${USER}; fi && \
	if getent passwd ${USER_ID}>/dev/null; then useradd -g ${USER} -G audio,video ${USER}; \
	else useradd -g ${USER} -u ${USER_ID} -G audio,video ${USER}; fi && \
	mkdir -p ${HOME_DIR}/work && \
	chown -R ${USER}:${USER} ${HOME_DIR}

WORKDIR ${HOME_DIR}

# Run everything after as non-privileged user
USER ${USER}
ENV PATH="${HOME_DIR}/.local/bin:${PATH}" \
	TZ=${TZ} \
	LANG=${LOCALE}

RUN pip install --no-cache-dir playwright ${PACKAGES} && \
    playwright install ${BROWSER}

Sul NAS ricordatevi di aggiungere al gruppo docker (già creato in fase d’installazione) l’utente con cui eseguirete gli script Playwright.
Per come è stata costruita l’immagine, Playwright non girerà con diritti di root, ma come normale utente. Sempre in fase di build, è possibile impostarne l’UID e il GID, in modo da farli coincidere con quelli dell’utente di cui sopra.

#!/bin/bash

export PACKAGES="requests"

# GID of docker group on NAS diskstation:   gid=65000(docker) uid=1000
docker image build --build-arg BROWSER=chromium --build-arg GROUP_ID=65542 --build-arg USER_ID=1026 --build-arg PACKAGES -t ubuntu:python-playwright -f Dockerfile .

Dopo aver generato l’immagine, per importarla sul NAS tramite la GUI Docker, è necessario produrre un file tar.gz con questo comando:

#!/usr/bin/env bash

docker image save ubuntu:python-playwright | gzip > ubuntu-python-playwright.tar.gz


Create il container, montando i percorsi necessari ai vostri script Playwright e stabilendo l’entrypoint che preferite.

Synology non permette l’esecuzione di script che interagiscano con container Docker, se non con diritti di root.

Per ovviare a questa limitazione, l’unico modo (almeno fino a DSM 6.2) è necessario cambiare i diritti di docker.sock in modo che gli utenti appartenenti al gruppo docker possano accedervi.
Se ancora non lo avete fatto, abilitate le connessioni ssh alla diskstation e connettendovi con l’utente admin eseguite:

ssh admin@diskstation
sudo chown root:docker /var/run/docker.sock

A questo punto, l’utente potrà eseguire e schedulare script che invochino il lancio di contenitori docker. Per esempio:

docker container start playwright
docker container wait playwright
Subscribe
Notificami

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.

2 Commenti
Newest
Oldest Most Voted
Inline Feedbacks
View all comments