8

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
  • 103,263

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
  • 103,263
3

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
  • 211
  • 1
  • 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
  • 119,640