PYAUTOGUI HEADLESS Docker mode without display in Python

Abhishek Vaid
3 min readDec 24, 2020

--

Hi Everyone, I am a Software Developer and I have made hundreds of advanced scrappers. Many times I come across sites on which it is much easier to use PYAUTOGUI for scrapping or roaming around the site.

Initially I faced a problem in running PYAUTOGUI headless without a display, but I finally have made a solution for the issue.

To run PYAUTOGUI headless you will need to make a docker using Xdisplay and provide path to virtual display to PYAUTOGUI to do so. Basically it will have the display memory run in the backend without actually having a display. Server deployable.

So lets begin to crack the code-

  1. Firstly you do not set your scrapper to Headless mode, the scrapper will by default become headless inside Docker which we will make latter in this post. It will use a virtual display and will be headless by default.
  2. Second you have to create a virtual Display as shown in the above image in side your scrapper.py file. The code for the same is down below.
from pyvirtualdisplay.display import Display
disp = Display(visible=True, size=(1366, 768), backend="xvfb", use_xauth=True)
disp.start()

As shown above, you first import Pyvirtual display. Then you set visible to True, Size resolution is the size of the display you are using on your device, set backend to xvfb and set use_xauth to true.

3. Start you display by disp.start(), as shown.

Now you have to provide PYAUTOGUI the path to your display. To do so use the code-

import Xlib.display
import pyautogui
pyautogui._pyautogui_x11._display = Xlib.display.Display(os.environ['DISPLAY'])

As shown you now after the display has started import pyautogui inside your scrapper.py script and provide pyautogui the path to Xdisplay.

After this you get your website and start using pyautogui as intended and make the whole scrapper or do whatever you wish to do with it. After this we make the docker image.

Now it is time for the Grand finale- to actually build a docker image to run PYAUTOGUI Headless

FROM ubuntu:bionic

RUN apt-get update && apt-get install python3 tesseract-ocr python3-pip curl unzip -yf
# Install Chrome
RUN apt-get update -y
RUN apt-get install -y dbus-x11
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /chrome.deb
RUN dpkg -i /chrome.deb || apt-get install -yf
RUN rm /chrome.deb
RUN apt-get -y install libssl1.0-dev
RUN curl https://chromedriver.storage.googleapis.com/87.0.4280.20/chromedriver_linux64.zip -o /usr/local/bin/chromedriver.zip
RUN cd /usr/local/bin && unzip chromedriver.zip
RUN chmod +x /usr/local/bin/chromedriver
USER root
RUN apt update
RUN apt-get install -y poppler-utils
RUN apt-get clean
RUN apt install -y python3 python3-pip
RUN DEBIAN_FRONTEND=noninteractive apt install -y python3-xlib python3-tk python3-dev
RUN apt install -y xvfb xserver-xephyr
python3-tk python3-dev
RUN Xvfb :99 -ac &
RUN export DISPLAY=:99
RUN pip3 install virtualenv
RUN mkdir /app
COPY requirements.txt /app
WORKDIR /app
RUN virtualenv .venv
RUN /bin/bash -c "source .venv/bin/activate"
RUN pip3 install -r requirements.txt
RUN apt-get install -y scrot
COPY . /app
ENTRYPOINT [ "python3", "scrapper.py" ]

So as shown in the above code you first get ubuntu operating system in your dockerfile because Xdisplay works on Ubuntu.

Then install python3 and chromedriver using the subsequent commands in the same script.

After this set user to root and install python3-xlib, python3-tk and python3-dev and do set DEBIAN_FRONTEND=noninteractive as they are needed to run pyautogui in ubuntu and docker does not allow intervention while installation.

After this install xvfb for virtual display backend capability.

Then RUN xvfb display on port 99 and assign port 99 in export display to docker.

After this RUN the virtual environment and provide path for activation using the subsequent commands in the script above.

Now a basic requirement.txt for installation should look something like the code below.

python-xlib==0.27
requests==2.24.0
opencv-python==4.2.0.34
PyVirtualDisplay==0.2.5
pyscreenshot==1.0
pillow==7.1.2
selenium==3.141.0
pyautogui==0.9.50
keyboard

As you can see I am using Pyautogui version 0.9.50. You can even use the latest one that won’t really cause any issues. Finally you can even install scrot if you intend to take screenshots while scrapping.

Well that is pretty much the dockerfile you will need to run PYAUTOGUI Headless.

--

--

Abhishek Vaid
Abhishek Vaid

Written by Abhishek Vaid

I am a Software Developer on my way to creating great Artificial Intelligent technologies.

Responses (4)