skip to Main Content

From this article, it states that windows 11 natively supports running of X11 and wayland applications on wsl.

I tried to do the same through a docker container, settinng the environment variable DISPLAY="host.docker.internal:0.0", and running a gui application (like gedit). But instead I got this error:

Unable to init server: Could not connect: Connection refused

Gtk-WARNING **: 17:05:50.416: cannot open display: host.docker.internal:0.0

2

Answers


  1. I stumbled upon your question while attempting the same thing as you are and acctually got it to work with aid of this blog post on Microsoft. I use a minimal Dockerfile based on Ubuntu and installs gedit:

    FROM ubuntu:22.04
    RUN apt update -y && apt install -y gedit
    CMD ["gedit"]
    

    Create the image the usual way, e.g. docker build . -t guitest:1.0

    On the WSL command line, start it like this:

     docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix 
                    -v /mnt/wslg:/mnt/wslg 
                    -e DISPLAY 
                    -e WAYLAND_DISPLAY 
                    -e XDG_RUNTIME_DIR 
                    -e PULSE_SERVER 
                    guitest:1.0
    

    I hope this is to good use for you as well.

    Login or Signup to reply.
  2. This answer is heavily based on what chrillof has said. Thanks for the excellent start!

    The critical things here for Docker Desktop users on Windows with WSL2 are that:

    1. The container host (i.e. docker-desktop-data WSL2 distribution) does not have a /tmp/.X11-unix itself. This folder is actually found in the /mnt/host/wslg/.X11-unix folder on the docker-desktop distribution which translates to /run/desktop/mnt/host/wslg/.X11-unix when running containers.
    2. There are no baked-in environment variables to assist you, so you need to specify the environment variables explicitly with these folders in mind.

    I found this GitHub issue where someone had to manually set environment variables which allowed me to connect the dots between what others experience directly on WSL2 and chrillof’s solution

    Therefore, modifying chrillof’s solution using PowerShell from the host, it looks more like:

    docker run -it -v /run/desktop/mnt/host/wslg/.X11-unix:/tmp/.X11-unix `
                   -v /run/desktop/mnt/host/wslg:/mnt/wslg `
                   -e DISPLAY=:0 `
                   -e WAYLAND_DISPLAY=wayland-0 `
                   -e XDG_RUNTIME_DIR=/mnt/wslg/runtime-dir `
                   -e PULSE_SERVER=/mnt/wslg/PulseServer `
                   guitest:1.0
    

    On my computer, it looks like
    this (demo of WSLg X11)

    To be clear, I have not checked if audio is functional or not, but this does allow you to avoid the installation of another X11 server if you already have WSL2 installed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search