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

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

mouse = Controller()
xValue = 470
yValue = 400


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), 2)

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


RUN pip install pynput
RUN pip install pyautogui

CMD [ "python", "./" ]

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

Traceback (most recent call last):
File “./”, line 3, in
import pyautogui
File “/usr/local/lib/python3.8/site-packages/pyautogui/”, line 241, in
import mouseinfo
File “/usr/local/lib/python3.8/site-packages/mouseinfo/”, line 223, in
_display = Display(os.environ[‘DISPLAY’])
File “/usr/local/lib/python3.8/”, 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.