X11 forwarding of a GUI app running in docker

前端 未结 4 716
南旧
南旧 2021-01-01 02:31

First off: I have read the answers to similar questions on SO, but none of them worked.

The situation:

  • App with GUI is running in a docker container (C
相关标签:
4条回答
  • 2021-01-01 02:51

    Ok, here is the thing:

    1) Log in to remote machine

    2) Check which display was set with echo $DISPLAY

    3) Run xauth list

    4) Copy the line corresponding to your DISPLAY

    5) Enter your docker container

    6) xauth add <the line you copied>*

    7) Set DISPLAY with export DISPLAY=<ip-to-host>:<no-of-display>

    *so far so good right?

    This was nothing new...however here is the twist: The line printed by xauth list for the login user looks something like this (in my case):

    <hostname-of-machine>/unix:<no-of-display> MIT-MAGIC-COOKIE-1 <some number here>
    

    Because i use the bridged docker setup, the X forwarding port is not listening locally, because the sshd is not running in the container. Change the line above to:

    <ip-of-host>:<no-of-display> MIT-MAGIC-COOKIE-1 <some number here>
    

    In essence: Remove the /unix part.

    <ip-of-host> is the IP address where the sshd is running.

    Set the DISPLAY variable as above.

    So the error was that the DISPLAY name in the environment variable was not the "same" as the entry in the xauth list / .Xauthority file and the client could therefor not authenticate properly.

    I switched back to an untrusted X11 forwarding setting.

    The X11UseLocalhost no setting in the sshd_config file however is important, because the incomming connection will come from a "different" machine (the docker container).

    0 讨论(0)
  • 2021-01-01 02:52

    Thanks so much @Lazarus535
    I found that for me adding the following to my docker command worked:
    --volume="$HOME/.Xauthority:/root/.Xauthority:rw"
    I found this trick here
    EDIT:
    As Lazarus pointed out correctly you also have to set the --net=host option to make this work.

    0 讨论(0)
  • 2021-01-01 02:53

    This works in any scenario.

    Install xhost if you don't have it. Then, in bash,

    export DISPLAY=:0.0
    xhost +local:docker
    

    After this run your docker run command (or whatever docker command you are running) with -e DISPLAY=$DISPLAY

    0 讨论(0)
  • 2021-01-01 03:08

    It works usually via https://stackoverflow.com/a/61060528/429476

    But if you are running docker with a different user than the one used for ssh -X into the server with; then copying the Xauthority only helped along with volume mapping the file.

    Example - I sshed into the server with alex user.Then ran docker after su -root and got this error

    X11 connection rejected because of wrong authentication.

    After copying the .XAuthoirty file and mapping it like https://stackoverflow.com/a/51209546/429476 made it work

    cp /home/alex/.Xauthority .
    
    docker run  -it --network=host --env DISPLAY=$DISPLAY  --privileged  \
     --volume="$HOME/.Xauthority:/root/.Xauthority:rw"  \
    -v /tmp/.X11-unix:/tmp/.X11-unix --rm <dockerimage>
    
    

    More details on wiring here https://unix.stackexchange.com/a/604284/121634

    0 讨论(0)
提交回复
热议问题