Docker Community Forums

Share and learn in the Docker community.

Docker with pyautogui

Hi, I try to automate some tasks by using pyinput and pyautogui. Now I wanna pack everything in a Docker container to that a friend can also run the code. The code is as follows in the file myfile.py:

import random
import time
import pyautogui
from pynput.mouse import Button, Controller

mouse = Controller()
xValue = 470
yValue = 400

time.sleep(5)

for i in range(4):
	sleepafterdoubleclick = random.uniform(0.4, 0.7)
	sleepafternextclick = random.uniform(0.7, 1.2)

	mouse.position = (xValue, yValue)
	time.sleep(0.1)
	mouse.click(Button.left, 2)
	time.sleep(sleepafterdoubleclick)
	pyautogui.press('right')

now I wrote a simple Dockerfile with the content:
FROM python:3.8

ADD myfile.py /

RUN pip install pynput
RUN pip install pyautogui

CMD [ "python", "./myfile.py" ]

After building the docker file I want to execute it and receive the error:

Traceback (most recent call last):
File “./NiklasMacLiker.py”, line 3, in
import pyautogui
File “/usr/local/lib/python3.8/site-packages/pyautogui/init.py”, line 241, in
import mouseinfo
File “/usr/local/lib/python3.8/site-packages/mouseinfo/init.py”, line 223, in
_display = Display(os.environ[‘DISPLAY’])
File “/usr/local/lib/python3.8/os.py”, line 675, in getitem
raise KeyError(key) from None
KeyError: ‘DISPLAY’

As I am new to Docker I have no clue, why it does not work properly. Any advice? Thanks in advance :slight_smile:

There is no xserver inside a container by default. Thus, no gui.

Check Dockerhub for images that provide no-vnc or xrdp support. Study their Dockerfile to get an idea what needs to be done.

On Linux systemes, another alternative is to mount the hosts x11 deamon socket inside the container. It would render the output on the hosts xserver. I am sure if you ask google consulting, it will provide many examples on how it’s done.