Is it possible to run Docker container and show its graphical application window on host?

10,144

Solution 1

We need to inform container about running X11 server on host with special syntax [1]:

docker run -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --user="$(id --user):$(id --group)" ubuntu:xclock

xclock on docker

where [2]:

-e, --env=[]
Set environment variables
-u, --user=""
Sets the username or UID used and optionally the groupname or GID for the specified command.
-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
Create a bind mount. If you specify, -v /HOST-DIR:/CONTAINER-DIR, Docker bind mounts /HOST-DIR in the host to /CONTAINER-DIR in the Docker container. If 'HOST-DIR' is omitted, Docker automatically creates the new volume on the host. The OPTIONS are a comma delimited list and can be:


Reference:

  1. https://forums.docker.com/t/x11-forwarding-with-v-on-docker-run-not-working/17708/4
  2. man docker-run

Complete reproducible solution:

mkdir ~/docker-xclock

cat > ~/docker-xclock/Dockerfile << EOF
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y x11-apps
CMD xclock
EOF

docker build -t ubuntu:xclock ~/docker-xclock

docker run -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --user="$(id --user):$(id --group)" ubuntu:xclock

Solution 2

if you're still getting error like:

No protocol specified
Error: Can't open display: :0

make sure you run

$ xhost local:docker

*-from https://github.com/jessfraz/dockerfiles/issues/6#issuecomment-266230114

Share:
10,144
N0rbert
Author by

N0rbert

Updated on September 18, 2022

Comments

  • N0rbert
    N0rbert over 1 year

    I have created the simpliest (minimal working example) Dockerfile to run graphical application on my Ubuntu 16.04 LTS host system with 19.10 insider container:

    mkdir ~/docker-xclock
    
    cat > ~/docker-xclock/Dockerfile << EOF
    FROM ubuntu:19.10
    RUN apt-get update
    RUN apt-get install -y x11-apps
    CMD xclock
    EOF
    

    Then created container with

    docker build -t ubuntu:xclock ~/docker-xclock
    

    When I try to run this container it shows the error about display:

    $ docker run ubuntu:xclock 
    Error: Can't open display: 
    

    What is wrong?

  • loxosceles
    loxosceles over 3 years
    Exits with error message docker: Error response from daemon: manifest for ubuntu:xclock not found: manifest unknown: manifest unknown
  • N0rbert
    N0rbert over 3 years
    It is 2021, so 19.10 is EOL. I updated answer above to use 20.04 LTS. Thanks!
  • Codebling
    Codebling almost 3 years
    The switch --user="$(id --user):$(id --group)" did nothing for me (Arch linux), I got the error No protocol specified Error: Can't open display: :0 with or without that option. Running xhost local:docker, as mentioned in the answer below, fixed this for me.
  • Rutger Hofste
    Rutger Hofste over 2 years
    Fails for me even after running the xhost command. :(
  • Rutger Hofste
    Rutger Hofste over 2 years
    Doesn't work for me on Ubuntu 20.10 :(