6

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?

N0rbert
  • 97,162
  • 34
  • 239
  • 423

3 Answers3

6

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
N0rbert
  • 97,162
  • 34
  • 239
  • 423
  • Exits with error message `docker: Error response from daemon: manifest for ubuntu:xclock not found: manifest unknown: manifest unknown` – loxosceles Jan 28 '21 at 17:46
  • It is 2021, so 19.10 is EOL. I updated answer above to use 20.04 LTS. Thanks! – N0rbert Jan 28 '21 at 19:31
  • 1
    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](https://askubuntu.com/a/1304947/52781), fixed this for me. – Codebling Jun 11 '21 at 20:43
  • Fails for me even after running the xhost command. :( – user426293 Aug 24 '21 at 21:20
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

IvanM
  • 181
  • 4
0

Create Dockerfile in the folder docker-xclock.

mkdir ~/docker-xclock

cat > ~/docker-xclock/Dockerfile << EOF
FROM ubuntu:19.04
ENV https_proxy="xxx"
ENV http_proxy="xxx"
ENV no_proxy="xxx"
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

NB: ubuntu:xclock put at the end of the command. Using X11 for GUI interaction .

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493