Is it possible to run Docker container and show its graphical application window on host?
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
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:
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
N0rbert
Updated on September 18, 2022Comments
-
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?
-
Admin almost 5 yearsIs "FROM ubuntu:19.04" correct or a typo?
-
Admin almost 4 yearsSee also Run simple X11 app in docker container on Ubuntu 20.04 if you installed docker with snap.
-
-
loxosceles over 3 yearsExits with error message
docker: Error response from daemon: manifest for ubuntu:xclock not found: manifest unknown: manifest unknown
-
N0rbert over 3 yearsIt is 2021, so 19.10 is EOL. I updated answer above to use 20.04 LTS. Thanks!
-
Codebling almost 3 yearsThe switch
--user="$(id --user):$(id --group)"
did nothing for me (Arch linux), I got the errorNo protocol specified Error: Can't open display: :0
with or without that option. Runningxhost local:docker
, as mentioned in the answer below, fixed this for me. -
Rutger Hofste over 2 yearsFails for me even after running the xhost command. :(
-
Rutger Hofste over 2 yearsDoesn't work for me on Ubuntu 20.10 :(