2

I am working on a django application that is served with gunicorn. In the application I am trying to generate screenshots of whole webpages with the help of selenium.

In my local environment (starting the application via python manage.py runserver) everything works fine as expected. But if I run it in a docker container, it suddenly doesn't work anymore.

My application looks like this:

    from selenium import webdriver
    driver = webdriver.PhantomJS()
    driver.set_window_size(1024, 768)
    driver.get(url)
    driver.save_screenshot(filepath)
    driver.quit()

During execution of the second line, I get after a few seconds the following error message and everything restarts. I never reach the window resize. Sadly there is not more information in the the error message.

[2018-03-19 01:41:36 +0000] [7] [CRITICAL] WORKER TIMEOUT (pid:19)
[2018-03-19 01:41:36 +0000] [19] [INFO] Worker exiting (pid: 19)
[2018-03-19 01:41:36 +0000] [28] [INFO] Booting worker with pid: 28

thanks to this answer I now get at least an error message:

app.views.add_views-77 ERROR 2018-03-19 02:36:24,668: Message: Can not connect to the Service phantomjs
Traceback (most recent call last):
[..]
    driver = webdriver.PhantomJS()
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 56, in __init__
    self.service.start()
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service phantomjs

I also experience the same behaviour when using Chrome (with chromium and chromium-webdriver both from the alpine package repository) as a webdriver.

This happens with docker-compose and without it.

Dockerfile:

FROM alpine:3.7

ENV PYTHONUNBUFFERED 1
ENV PHANTOMJS_VERSION 2.1.1

RUN mkdir /code
WORKDIR /code
COPY Pipfile Pipfile.lock ./

RUN apk update && \
 apk add python3 postgresql-libs jpeg-dev git curl fontconfig && \
 apk add --virtual .build-deps gcc python3-dev musl-dev postgresql-dev zlib-dev && \
 mkdir -p /opt && \
 pip3 install --no-cache-dir pipenv && \
 pipenv install --system && \
 curl -L https://github.com/Overbryd/docker-phantomjs-alpine/releases/download/2.11/phantomjs-alpine-x86_64.tar.bz2 | tar xjC /opt && \
 ln -s /opt/phantomjs/phantomjs /usr/bin/phantomjs && \
 apk --purge del .build-deps

COPY . ./

RUN adduser -D noroot
USER noroot  
EXPOSE $PORT

CMD gunicorn app.wsgi --bind 0.0.0.0:$PORT --log-file -

What could be the problem here?

Simon Lenz
  • 2,732
  • 5
  • 33
  • 39

0 Answers0