7

I am following this example of how to run an X11 app in a docker container. I am on Ubuntu 20.04 and using X11:

$ echo $XDG_SESSION_TYPE
x11

My Dockerfile:

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y x11-apps
ARG user=hakon
ARG home=/home/$user
RUN groupadd -g 1000 $user
RUN useradd -d $home -s /bin/bash -m $user -u 1000 -g 1000 \
        && echo $user:ubuntu | chpasswd \
        && adduser $user sudo
WORKDIR $home
USER $user
ENV HOME $home
COPY entrypoint.sh .
ENTRYPOINT ["./entrypoint.sh"] 

where entrypoint.sh is:

echo "DISPLAY=$DISPLAY"
xclock  # <-- This is the X11 application I am testing with. It shows a clock in a Window
echo "Done."
exec bash

I build the image using:

$ docker build -t gui-test-ubuntu-2004 .

Then run the container with:

$ docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY \
       -h $HOSTNAME -v $HOME/.Xauthority:/home/hakon/.Xauthority gui-test-ubuntu-2004

The output is:

DISPLAY=:0
Error: Can't open display: :0
Done.

and the xclock gui window is not showing. What am I missing here?

Håkon Hægland
  • 3,843
  • 12
  • 44
  • 78
  • Does this answer your question? [Is it possible to run Docker container and show its graphical application window on host?](https://askubuntu.com/questions/1161646/is-it-possible-to-run-docker-container-and-show-its-graphical-application-window) – N0rbert Jun 11 '20 at 08:14
  • @N0rbert Thanks for the link! It still does not work with `--user 1000:1000`. I tried this command: `docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY --user 1000:1000 gui-test-ubuntu-2004`. Still the same error message: `Error: Can't open display: :0` – Håkon Hægland Jun 11 '20 at 08:27
  • Try to simply repeat commands from my Q&A then adapt yours. The 1000 is not really constant, it is value of `id -u` and `id -g` (user and group identifiers). – N0rbert Jun 11 '20 at 08:28
  • If I try to build the `Dockerfile` in your [example](https://askubuntu.com/q/1161646/156688), I get error: `Err:5 http://archive.ubuntu.com/ubuntu disco Release 404 Not Found [IP: 91.189.88.142 80]` – Håkon Hægland Jun 11 '20 at 08:33
  • Thanks, updated it to actual 19.10. – N0rbert Jun 11 '20 at 08:34
  • Ok with 19.10 it builds the image fine. But when I run the container, I still get error: `Error: Can't open display: :0` – Håkon Hægland Jun 11 '20 at 08:37
  • I am on Ubuntu 20.04, maybe that's the issue? – Håkon Hægland Jun 11 '20 at 08:38
  • It may be in the case when you are running Wayland. Try to switch to Xorg on login screen. – N0rbert Jun 11 '20 at 08:58
  • I think I am already on Xorg. If I type `echo $XDG_SESSION_TYPE` the output is `x11` – Håkon Hægland Jun 11 '20 at 09:10
  • How did you install `docker` ? I installed it with `snap` – Håkon Hægland Jun 11 '20 at 10:17
  • 1
    Snap may be a reason, I have installed it as `docker.io` package from default repositories. Then added my user to the *docker* group. – N0rbert Jun 11 '20 at 10:40
  • 1
    Yes, I think that `snap` might be the issue here, see [this](https://github.com/mviereck/x11docker/issues/256) issue: *"snap causes several restictions. --hostdisplay does not work because it is not possible to share unix sockets from host, in this case the X unix socket in /tmp/.X11-unix"* – Håkon Hægland Jun 11 '20 at 10:47
  • I had to use `$XAUTHORITY` instead of `$HOME/.Xauthority` for the volume mount host location. – Daniel Stevens Oct 29 '20 at 07:36

3 Answers3

7

The problem is most likely that you installed docker with snap. According to mviereck the maintainer of x11docker

snap causes several restictions. --hostdisplay does not work because it is not possible to share unix sockets from host, in this case the X unix socket in /tmp/.X11-unix

A workaround is to an Xephyr X server with x11docker. Instead of running docker run ... gui-test-ubuntu-2004, use:

x11docker --xephyr gui-test-ubuntu-2004

The second alternative is to remove the snap installation of docker, see this answer, and then reinstall docker with apt-get install:

  1. Remove docker:

     sudo snap remove docker
    
  2. Go to https://download.docker.com/linux/ubuntu/dists/ and choose your Ubuntu version, then browse to pool/stable/ and download the 3 .deb files:

    • containerd.io_1.2.13-2_amd64.deb

    • docker-ce_19.03.11_3-0_ubuntu-focal_amd64.deb

    • docker-ce-cli_19.03.11_3-0_ubuntu-focal_amd64.deb

  3. Install them in the following order:

     sudo apt-get install ./containerd.io_1.2.13-2_amd64.deb
     sudo apt-get install ./docker-ce-cli_19.03.11_3-0_ubuntu-focal_amd64.deb
     sudo apt-get install ./docker-ce_19.03.11_3-0_ubuntu-focal_amd64.deb
    
  4. Add your user to the docker group to avoid having to type sudo to run docker:

    sudo addgroup --system docker
    sudo usermod -a -G docker $USER
    
  5. Restart the computer at this point to activate the new user and group settings

  6. Test the docker installation by running :

    docker run hello-world
    

You should now be able to run the original docker run command:

docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY \
       -h $HOSTNAME -v $HOME/.Xauthority:/home/hakon/.Xauthority gui-test-ubuntu-2004
Håkon Hægland
  • 3,843
  • 12
  • 44
  • 78
  • For me this was not the answer, I simply ran `xhost + local:docker` first and it worked. Not sure how much of a security risk this is? – Dan Bolser Nov 16 '21 at 12:53
  • Thank you! Installing via snap was indeed the problem. I simply removed the snap and installed via apt. – Alqio Jan 12 '23 at 09:11
5

Type xhost + local:docker before running docker.

Dan Bolser
  • 103
  • 4
Kjeld Flarup
  • 419
  • 6
  • 10
1

I was testing this but it was raising the error. standard_init_linux.go:228: exec user process caused: exec format error

My docker was installed following the instructions here: https://docs.docker.com/engine/install/ubuntu/

I added the the #!/bin/bash on the first line of your entrypoint.sh and it's working for me.