linuxserver / docker-webtop

Ubuntu, Alpine, Arch, and Fedora based Webtop images, Linux in a web browser supporting popular desktop environments.
GNU General Public License v3.0
1.86k stars 181 forks source link

[BUG] Can't use `pyautogui` inside container #220

Closed joellidin closed 8 months ago

joellidin commented 8 months ago

Is there an existing issue for this?

Current Behavior

When running lscr.io/linuxserver/webtop:ubuntu-xfce and trying to run pyautogui inside the container I get the following error:

Traceback (most recent call last):
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/Xlib/support/unix_connect.py", line 76, in get_socket
    s.connect('/tmp/.X11-unix/X%d' % dno)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/pyautogui/__init__.py", line 246, in <module>
    import mouseinfo
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/Xlib/display.py", line 80, in __init__
    self.display = _BaseDisplay(display)
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/Xlib/display.py", line 62, in __init__
    display.Display.__init__(*(self, ) + args, **keys)
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/Xlib/protocol/display.py", line 58, in __init__
    self.socket = connect.get_socket(name, host, displayno)
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/Xlib/support/connect.py", line 76, in get_socket
    return mod.get_socket(dname, host, dno)
  File "/config/.pyenv/versions/3.10.13/lib/python3.10/site-packages/Xlib/support/unix_connect.py", line 78, in get_socket
    raise error.DisplayConnectionError(dname, str(val))
Xlib.error.DisplayConnectionError: Can't connect to display ":1.0": [Errno 2] No such file or directory

I have tried searching around the web and trying out different suggestions but nothing worked for me. I can use pyautogui in docker-headless-vnc-container, but then I run into other issues I do not have with webtop. Since it works in another vnc container I would suspect it to work if I configure it correctly?

Expected Behavior

No response

Steps To Reproduce

With this Dockerfile:

FROM lscr.io/linuxserver/webtop:ubuntu-xfce

# Install system dependencies for pyautogui, poetry, and pyenv
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-tk \
    python3-dev \
    scrot \
    python3-xlib \
    make \
    build-essential \
    libssl-dev \
    zlib1g-dev \
    libbz2-dev \
    libreadline-dev \
    libsqlite3-dev \
    wget \
    curl \
    llvm \
    libncurses5-dev \
    libncursesw5-dev \
    xz-utils \
    tk-dev \
    libffi-dev \
    liblzma-dev \
    python3-openssl \
    git \
 && rm -rf /var/lib/apt/lists/*

And this docker-compose.yaml:

---
services:
  webtop:
    build: .
    container_name: webtop
    security_opt:
      - seccomp:unconfined #optional
    privileged: true
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Etc/UTC
      # - SUBFOLDER=/ #optional
      # - TITLE=Webtop #optional
    volumes:
      - /srv/webtop:/config
      - /var/run/docker.sock:/var/run/docker.sock #optional
    ports:
      - 3000:3000
    devices:
      - /dev/dri:/dev/dri #optional
    shm_size: "1gb" #optional
    restart: unless-stopped

Then inside container run pip install pyautogui and then using the repl python:

>>> import pyautogui

Environment

- OS: Ubuntu 22.04
- How docker service was installed: Through download.docker

CPU architecture

x86-64

Docker creation

docker compose up -d

Container logs

[migrations] started
[migrations] no migrations found
───────────────────────────────────────

      ██╗     ███████╗██╗ ██████╗
      ██║     ██╔════╝██║██╔═══██╗
      ██║     ███████╗██║██║   ██║
      ██║     ╚════██║██║██║   ██║
      ███████╗███████║██║╚██████╔╝
      ╚══════╝╚══════╝╚═╝ ╚═════╝

   Brought to you by linuxserver.io
───────────────────────────────────────

To support LSIO projects visit:
https://www.linuxserver.io/donate/

───────────────────────────────────────
GID/UID
───────────────────────────────────────

User UID:    1000
User GID:    1000
───────────────────────────────────────

**** adding /dev/dri/renderD128 to video group render with id 110 ****
**** adding /dev/dri/card0 to video group video with id 44 ****
[custom-init] No custom files found, skipping...

Xvnc KasmVNC 1.2.0 - built Jan 20 2024 18:59:10
Copyright (C) 1999-2018 KasmVNC Team and many others (see README.me)
See http://kasmweb.com for information on KasmVNC.
Underlying X server release 12014000, The X.Org Foundation

[ls.io-init] done.
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
 2024-01-25 12:22:47,509 [INFO] websocket 0: got client connection from 127.0.0.1
 2024-01-25 12:22:47,517 [PRIO] Connections: accepted: @192.168.0.1_1706185367.510030::websocket
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
 2024-01-25 12:23:20,832 [INFO] websocket 1: got client connection from 127.0.0.1
 2024-01-25 12:23:20,840 [PRIO] Connections: accepted: @192.168.0.1_1706185400.832701::websocket
failed to load listeners: can't create unix socket /var/run/docker.sock: device or resource busy
 2024-01-25 12:23:26,060 [PRIO] Connections: closed: @192.168.0.1_1706185367.510030::websocket (Clean disconnection)
github-actions[bot] commented 8 months ago

Thanks for opening your first issue here! Be sure to follow the relevant issue templates, or risk having this issue marked as invalid.

thelamer commented 8 months ago
sudo apt-get update && sudo apt-get install -y python3-pip python3-tk python3-dev
sudo pip3 install pyautogui
touch $HOME/.Xauthority
echo "import pyautogui" > test.py
echo "pyautogui.press('enter')" >> test.py
python3 test.py
joellidin commented 8 months ago

@thelamer Thanks for the reply. Unfortunately, I still get

Xlib.error.DisplayConnectionError: Can't connect to display ":1.0": [Errno 2] No such file or directory
thelamer commented 8 months ago

Yeah you would need to run this from the web session or as the user abc with the with-contenv wrapper to get the env variables.

joellidin commented 8 months ago

I did run this inside the container from the web session. I don't really now what the with-contenv wrapper is and cand find any information about it. Could you provide a reference so I can read about it?

thelamer commented 8 months ago

This is not a support forum, I can move the mouse and send key events with pyautogui:

sudo apt-get update && sudo apt-get install -y python3-pip python3-tk python3-dev
sudo pip3 install pyautogui
touch $HOME/.Xauthority
echo "import pyautogui" > test.py
echo "pyautogui.press('enter')" >> test.py
echo "print(pyautogui.position())" >> test.py
echo "pyautogui.moveTo(100, 200)" >> test.py
echo "print(pyautogui.position())" >> test.py
python3 test.py
Xlib.xauth: warning, no xauthority details available
Xlib.xauth: warning, no xauthority details available
Point(x=1093, y=582)
Point(x=100, y=200)
cat /command/with-contenv
#!/command/execlineb -S0

ifelse
{
  importas -D 0 S6_KEEP_ENV S6_KEEP_ENV
  eltest 0${S6_KEEP_ENV} -eq 0
}
{
  emptyenv -p
  s6-envdir -Lfn -- /run/s6/container_environment
  exec
  $@
}

$@