Skip to content

Instantly share code, notes, and snippets.

@sisco70
Last active May 28, 2021 13:25
Show Gist options
  • Save sisco70/87a72961f7299a3e0c2609443c8e0d51 to your computer and use it in GitHub Desktop.
Save sisco70/87a72961f7299a3e0c2609443c8e0d51 to your computer and use it in GitHub Desktop.
Dockerfile for Python Playwright (Ubuntu 20.04LTS). You can choose the browser to install. Custom user UID:GID when you build the image, useful for Synology Docker.
#!/usr/bin/env bash
docker image build --build-arg BROWSER=chromium --build-arg TZ='Europe/Rome' --build-arg GROUP_ID=65542 --build-arg USER_ID=1026 -t ubuntu:python-playwright -f Dockerfile .
#!/usr/bin/env bash
docker image save ubuntu:python-playwright | gzip > ubuntu-python-playwright.tar.gz
#
# 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 TZ='Europe/Rome' \
# --build-arg GROUP_ID=65542 --build-arg USER_ID=1026 -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
# Optional Timezone set by default at build time..
ARG TZ
ENV TZ=$TZ
# Optional arg, select which browser install, if undefined install all
ARG BROWSER
# Optional arg, docker user UID:GID
ARG USER_ID
ARG GROUP_ID
#
ARG USER=play
ARG HOME_DIR=/home/${USER}
ARG DEBIAN_FRONTEND=noninteractive
# LABEL about the custom image
LABEL maintainer="Francesco Guarnieri"
LABEL version="0.2"
LABEL description="Ubuntu 20.04LTS docker image for Python and Playwright"
# Install WebKit dependencies
RUN apt-get update && \
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 --quiet --install /usr/bin/pip pip /usr/bin/pip3 1 && \
update-alternatives --quiet --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}"
RUN pip install --no-cache-dir playwright && \
playwright install ${BROWSER}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment