A GUI app in a Docker container on Linux says Gtk cannot open display :0
Gtk: cannot open display: :0Short answer
A container has no screen of its own. To draw on the host's X server it needs the host's DISPLAY value, the X11 socket directory from /tmp/.X11-unix mounted inside, and permission from the X server for the container's user. Missing any one of the three gives cannot open display. Add them to docker run, and allow access with xhost.
Fix it fast
1Find out which cause you have
Compare the display the host uses with what the container sees, and check that the socket for that display exists on the host.
echo "host DISPLAY=$DISPLAY" ls -l /tmp/.X11-unix/ docker run --rm IMAGE sh -c 'echo "container DISPLAY=$DISPLAY"; ls /tmp/.X11-unix'2Match what you see, then jump to its fix
- 1
container DISPLAY= prints an empty value, or :0 while the host prints :1DISPLAY is empty in the container, or names a different display than the hostFix - 2
ls /tmp/.X11-unix inside the container prints nothing or says no such file, while the host lists X0 or X1The X11 socket directory is not shared with the containerFix - 3
DISPLAY and the socket are both correct inside the container, and the program still cannot open the displayThe X server does not allow the container's user to connectFix
- 1
What does the error look like?
Gtk: cannot open display: :0cannot open display: :0
Cause 1 of 3
DISPLAY is empty in the container, or names a different display than the host
1Check
A display name says where the X server is: an empty host part means a local connection on this machine, and the number picks the display. A container does not inherit the host's variable, and an image that hard-codes :0 fails when your session runs on another number.
echo "$DISPLAY"
docker run --rm IMAGE printenv DISPLAY2Fix
Pass the host's own value into the container.
docker run --rm \
-e DISPLAY="$DISPLAY" \
IMAGE APP3Verify
The container now prints the same value as the host.
docker run --rm -e DISPLAY="$DISPLAY" IMAGE printenv DISPLAYConfirmed by xorg.freedesktop.org
Cause 2 of 3
The X11 socket directory is not shared with the container
1Check
On the host, the X server listens on a Unix domain socket named after the display number, /tmp/.X11-unix/X0 for display :0. A container has its own file system, so that socket is invisible unless the directory is bind-mounted.
ls -l /tmp/.X11-unix/
docker run --rm IMAGE ls -l /tmp/.X11-unix2Fix
Bind-mount the directory at the same path, next to the DISPLAY variable.
docker run --rm \
-e DISPLAY="$DISPLAY" \
-v /tmp/.X11-unix:/tmp/.X11-unix \
IMAGE APP3Verify
The container lists the same socket as the host.
docker run --rm -v /tmp/.X11-unix:/tmp/.X11-unix IMAGE ls -l /tmp/.X11-unixConfirmed by xorg.freedesktop.org
Cause 3 of 3
The X server does not allow the container's user to connect
1Check
The X server keeps a list of who may connect, and xhost shows whether access control is on and what is on the list. Processes in a container usually run as root, which is not the user of your desktop session.
xhost2Fix
Add the container's user to the list. The server interpreted name si:localuser:USER names one local user. The broader form +local: allows every local connection, so keep it for a short test.
xhost +si:localuser:root
# a broad alternative for a quick test:
# xhost +local:3Verify
Start the container again. When you are done, remove the entry so access control is back to its normal state.
docker run --rm -e DISPLAY="$DISPLAY" -v /tmp/.X11-unix:/tmp/.X11-unix IMAGE APP
xhost -si:localuser:rootConfirmed by xorg.freedesktop.org
How do I stop it coming back?
- Keep one working docker run line, or a Compose service with environment DISPLAY and the socket volume, in the project README.
- Remove xhost entries when the session ends, and never use plain xhost + outside a throwaway test: it turns access control off for everyone.
- Run the app as the same user as the desktop session where you can, so there is less to allow.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Security
A GUI program over SSH says it cannot open the display
cannot open display:
ssh must forward X11 and the remote sshd must allow it.
3 causes, each with a fixWeb apps
Playwright cannot open a headed browser on a Linux server or in Docker
npx playwright test --headed shows no window on a Linux server
A headed browser needs a display, and a Linux server or container has none.
3 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fix