Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

3.4 KB
Raw Blame
ARG ACR_CACHE_PREFIX
FROM ${ACR_CACHE_PREFIX}ubuntu:jammy

ARG DEBIAN_FRONTEND=noninteractive
ARG TZ=America/Los_Angeles
ARG DOCKER_IMAGE_NAME_TEMPLATE="mcr.microsoft.com/playwright:v%version%-jammy"
ARG NODE_VERSION=24 # autogenerated via ./update-playwright-node.mjs
ARG UBUNTU_MIRROR_PREFIX

ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8

# === INSTALL Node.js ===

RUN --mount=type=secret,id=npmrc,target=/root/.npmrc,required=false \
    find /etc/apt -type f \( -name '*.list' -o -name '*.sources' \) \
      -exec sed -i "s|http://archive.ubuntu.com|http://${UBUNTU_MIRROR_PREFIX}archive.ubuntu.com|g; s|http://ports.ubuntu.com|http://${UBUNTU_MIRROR_PREFIX}ports.ubuntu.com|g" {} + && \
    apt-get update && \
    apt-get install -y curl wget gpg ca-certificates && \
    mkdir -p /etc/apt/keyrings && \
    curl -sL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_VERSION}.x nodistro main" >> /etc/apt/sources.list.d/nodesource.list && \
    apt-get update && \
    apt-get install -y nodejs && \
    # Feature-parity with node.js base images.
    # zstd is used by actions/cache.
    apt-get install -y --no-install-recommends git openssh-client zstd && \
    npm install -g yarn && \
    # clean apt cache
    rm -rf /var/lib/apt/lists/* && \
    # Create the pwuser
    adduser pwuser

# === BAKE BROWSERS INTO IMAGE ===
# Browsers are split into separate layers to enable parallel pulls.

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

# 1. Add tip-of-tree Playwright package to install its browsers.
#    The package should be built beforehand from tip-of-tree Playwright.
COPY ./playwright-core.tar.gz /tmp/playwright-core.tar.gz

# 2. Set up playwright-core and install all system dependencies in one layer.
RUN install -d -m 777 /ms-playwright /ms-playwright/.links && \
    mkdir /ms-playwright-agent && \
    cd /ms-playwright-agent && npm init -y && \
    npm i /tmp/playwright-core.tar.gz && \
    npm exec --no -- playwright-core mark-docker-image "${DOCKER_IMAGE_NAME_TEMPLATE}" && \
    npm exec --no -- playwright-core install-deps && \
    # Workaround for https://github.com/microsoft/playwright/issues/27313
    # While the gstreamer plugin load process can be in-process, it ended up throwing
    # an error that it can't have libsoup2 and libsoup3 in the same process because
    # libgstwebrtc is linked against libsoup2. So we just remove the plugin.
    if [ "$(uname -m)" = "aarch64" ]; then \
        rm /usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstwebrtc.so; \
    else \
        rm /usr/lib/x86_64-linux-gnu/gstreamer-1.0/libgstwebrtc.so; \
    fi && \
    rm -rf /var/lib/apt/lists/*

# 3. Install each browser binary in its own layer for parallel pulling.
# Note: installing chromium also installs chromium-headless-shell and ffmpeg.
RUN cd /ms-playwright-agent && \
    npm exec --no -- playwright-core install chromium && \
    chmod -R 777 /ms-playwright/chromium-* /ms-playwright/chromium_headless_shell-* /ms-playwright/ffmpeg-*

RUN cd /ms-playwright-agent && \
    npm exec --no -- playwright-core install firefox && \
    chmod -R 777 /ms-playwright/firefox-*

RUN cd /ms-playwright-agent && \
    npm exec --no -- playwright-core install webkit && \
    rm /tmp/playwright-core.tar.gz && \
    rm -rf /ms-playwright-agent && \
    rm -rf ~/.npm/ && \
    chmod -R 777 /ms-playwright/webkit-*