Installing and using browser on alpine-based docker images

Has anyone had success using browser on alpine-based images? I’m using ppodgorsek/robot-framework as the base image and created a new image with browser and a couple other things in it. Building the image is successful but when running a simple example test using browser, it seems to be missing some libraries. Any ideas if this would be fixable or am I better off using debian-based images?

Dockerfile:

FROM ppodgorsek/robot-framework:latest

USER 0:0

RUN apk update && apk upgrade && \
    apk add bash curl sudo build-base unixodbc-dev unixodbc freetds-dev gcc g++ curl gnupg iputils nodejs libressl-dev alsa-lib libbz2 && \
    apk add --no-cache tzdata && \
	
COPY ./requirements.txt /tmp/requirements.txt
RUN pip install -U pip
RUN pip3 install -r /tmp/requirements.txt

RUN rfbrowser init

RUN rm -rf /tmp/*

ENV PYTHONPATH=$PYTHONPATH:/opt/robotframework/tests

USER ${ROBOT_UID}:${ROBOT_GID}

requirements.txt:

pytz
robotframework-jsonlibrary
robotframework-whitelibrary
RESTinstance
diskcache
pymssql
pyodbc
robotframework-browser

test.robot:

*** Settings ***
Library   Browser

*** Test Cases ***
Example Test
    New Page    https://playwright.dev
    Get Text    h1    ==    🎭 Playwright

Then building and running it:

$ docker build . -t robot-browser
$ docker run --rm -it -v "/path/to/testfolder:/opt/robotframework/tests" robot-browser:latest
==============================================================================
Tests
==============================================================================
Tests.Test
==============================================================================
[ WARN ] Keyword 'Take Screenshot' could not be run on failure:
Error: Tried to take screenshot, but no page was open.
Example Test                                                          | FAIL |
Error: browserType.launch: Protocol error (Browser.getVersion): Browser closed.
==================== Browser output: ====================
<launching> /usr/local/lib/python3.9/site-packages/Browser/wrapper/node_modules/playwright/.local-browsers/chromium-901522/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --dis
able-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --d
isable-extensions --disable-features=TranslateUI,BlinkGenPropertyTrees,ImprovedCookieControls,SameSiteByDefaultCookies,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose --allow-pre-commit-input --disable-hang-monitor --di
sable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=bas
ic --use-mock-keychain --no-service-autorun --user-data-dir=/tmp/playwright_chromiumdev_profile-MckehO --remote-debugging-pipe --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPoi
nterType=4,availablePointerTypes=4 --no-sandbox --no-startup-window
    [ Message content over the limit has been removed. ]
...e=basic --use-mock-keychain --no-service-autorun --user-data-dir=/tmp/playwright_chromiumdev_profile-MckehO --remote-debugging-pipe --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,pr
imaryPointerType=4,availablePointerTypes=4 --no-sandbox --no-startup-window
<launched> pid=64
[pid=64][err] /usr/local/lib/python3.9/site-packages/Browser/wrapper/node_modules/playwright/.local-browsers/chromium-901522/chrome-linux/chrome: /usr/lib/libasound.so.2: no version information available (required by /usr/local/lib/pytho
n3.9/site-packages/Browser/wrapper/node_modules/playwright/.local-browsers/chromium-901522/chrome-linux/chrome)
[pid=64][err] /usr/local/lib/python3.9/site-packages/Browser/wrapper/node_modules/playwright/.local-browsers/chromium-901522/chrome-linux/chrome: /usr/lib/libasound.so.2: no version information available (required by /usr/local/lib/pytho
n3.9/site-packages/Browser/wrapper/node_modules/playwright/.local-browsers/chromium-901522/chrome-linux/chrome)
[pid=64][err] /usr/local/lib/python3.9/site-packages/Browser/wrapper/node_modules/playwright/.local-browsers/chromium-901522/chrome-linux/chrome: Relink `/usr/lib/libbz2.so.1' with `/usr/glibc-compat/lib/libc.so.6' for IFUNC symbol `strc
at'
============================================================
------------------------------------------------------------------------------
Tests.Test                                                            | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Tests                                                                 | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Output:  /opt/robotframework/reports/output.xml
Log:     /opt/robotframework/reports/log.html
Report:  /opt/robotframework/reports/report.html

On Browser upstream and to my understanding even at Playwright only debian based variants are tested. In general I can offer no help on even getting the browsers to run in Alpine.

Also as an aside, even in general for python projects oftentimes debian based images can be faster / slimmer than alpine based. Building python packages inside alpine slows down the container building quite a lot. See The best Docker base image for your Python application (February 2021) for some (a bit old) examples.

So if you can, please try a debian / ubuntu based base-image, if you can’t, lets hope that somebody else has experience getting the library run on Alpine.

Well with much searching and pulling bits of info from various websites I did manage to get it *working*

There was a big BUT though, you need to add the --no-sandbox option to chrome, I can’t remember which one of these worked, but it was one of them, in the end I never used it:

# ${BROWSER}		Headless Chrome		--no-sandbox
# ${BROWSER_OPT}	options=add_argument("--no-sandbox")

I did this quite a while ago so the versions are quite old and I had trouble getting it to work with newer versions too, in the end I gave up with it as the --no-sandbox was a deal breaker for my purposes (I was wanting to create rfswarm-agent docker images to support >10 robots)

If you still want to continue this is what I had working, from what I understand the trick was setting the screen resolution and using Xvfb to create a virtual xwindows environment so that the browser could run:

Dockerfile:

FROM python:3.7-alpine3.9

ENV SCREEN_WIDTH 1280
ENV SCREEN_HEIGHT 720
ENV SCREEN_DEPTH 16
ENV DEPS="\
    chromium \
    chromium-chromedriver \
    udev \
    xvfb \
"

COPY requirements.txt /tmp/requirements.txt
COPY entry_point.sh /opt/bin/entry_point.sh

RUN apk update ;\
    apk add --no-cache ${DEPS} ;\
    pip install --no-cache-dir -r /tmp/requirements.txt ;\
    # Chrome requires docker to have cap_add: SYS_ADMIN if sandbox is on.
    # Disabling sandbox and gpu as default.
    sed -i "s/self._arguments\ =\ \[\]/self._arguments\ =\ \['--no-sandbox',\ '--disable-gpu'\]/" $(python -c "import site; print(site.getsitepackages()[0])")/selenium/webdriver/chrome/options.py ;\
    # List packages and python modules installed
    apk info -vv | sort ;\
    pip freeze ;\
    # Cleanup
    rm -rf /var/cache/apk/* /tmp/requirements.txt

ENTRYPOINT [ "/opt/bin/entry_point.sh" ]

entry_point.sh:

#!/bin/sh

GEOMETRY="${SCREEN_WIDTH}x${SCREEN_HEIGHT}x${SCREEN_DEPTH}"
Xvfb :99 -screen 0 $GEOMETRY &
export DISPLAY=:99

robot "$@"

requirements.txt

robotframework==3.1.1
robotframework-selenium2library==3.0.0
robotframework-seleniumlibrary==3.3.1
selenium==3.141.0

Hopefully you find this useful,

Dave.

Thanks for the help. The base image in question (ppodgorsek/robot-framework) already has Chrome and Firefox there, so the problem is only with making the Browser library itself working. Maybe worth looking though whether switching to one of the existing Debian-based images would make things easier while not breaking anything in our test environments.

As far as I remember, the browsers installed in that image are required for Selenium library.

Browser Library does not require pre installed browsers, as far as I know.

Hi Matti,

From the image you had I don’t see anywhere where you are setting a screen resolution or (virtual) display, in order to open a browser (even in headless mode) seems to require some kind of display driver. perhaps you can adapt the entry_point.sh from my example to make yours work.

Dave.

This is the entrypoint for the base image (as well as the one I derived from it) docker-robot-framework/run-tests-in-virtual-screen.sh at master · ppodgorsek/docker-robot-framework · GitHub . It does use xvfb and sets the screen size too.

I noticed my baseimage has added the Browser library in their commits since last release. Tried it without success. Filed a bug report to them. If it doesn’t seem to get resolved I’ll start porting my own derived image to debian. Or maybe start from an existing robot image that is already debian based.