How to show GUI apps from docker desktop container on windows 11
Asked Answered
S

3

8

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, setting 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
Senior answered 23/7, 2022 at 17:12 Comment(0)
P
11

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.

Protium answered 9/2, 2023 at 0:7 Comment(1)
Thanks so much for posting this! I've been trying to run GUI apps like Rviz while using ROS2 on WSL2 + Docker and this is just what I needed. Much appreciated!Isborne
V
10

I stumbled upon your question while attempting the same thing as you are and actually got it to work with the aid of this blog post on Microsoft. I use a minimal Dockerfile based on Ubuntu and install 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.

Veal answered 29/9, 2022 at 20:25 Comment(3)
am i right to say that for this to work, we have to run docker from a install distro on WSL, instead of running docker from docker desktop?Senior
I ran it on an installed distro in WSL. I don't know how the interaction is with docker desktop, however, I'm pretty sure it was visible from the docker desktop gui as a running container as wellVeal
I get this error when running using docker-desktop on Win10 : (gedit:1): Gtk-WARNING **: 13:51:26.986: cannot open display:Ori
S
0

Not sure if it helps you, but I'm using Xlaunch for UI in docker. Here is an example of usage gui app in docker

Subak answered 10/9, 2023 at 13:8 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Woodyard
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewUpthrust

© 2022 - 2024 — McMap. All rights reserved.