Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running X server using Docker

Tags:

docker

x11

I've been trying to get the following to work without success:

Dockerfile

FROM ubuntu:latest

#To install without any interactive dialogue
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update
# Installing Nautilus File Manager
RUN apt-get install nautilus -y

CMD ["nautilus"]

Then running with:

sudo docker build -t nautilus-ubuntu .

sudo docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix nautilus-ubuntu

The error that I am getting is:

Unable to init server: Could not connect: Connection refused

This seems to have been asked heavily before but I've been trying for an hour all I could come across and there's nothing other than xhost + (which is insecure and I don't want to use) that can make this work.

One thing to note is that I am trying to get this to work in a way that I can build it and launch the target GUI app immediately, without having to log in to a bash session and do further things.

like image 867
mangotango Avatar asked Jun 20 '26 13:06

mangotango


1 Answers

I found the answer here.

These should be added to the Dockerfile:

# Arguments picked from the command line!
ARG user
ARG uid
ARG gid

#Add new user with our credentials
ENV USERNAME ${user}
RUN useradd -m $USERNAME && \
        echo "$USERNAME:$USERNAME" | chpasswd && \
        usermod --shell /bin/bash $USERNAME && \
        usermod  --uid ${uid} $USERNAME && \
        groupmod --gid ${gid} $USERNAME

USER ${user}

WORKDIR /home/${user}

Then, creating xauth file:

xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f /tmp/.docker.xauth nmerge -

Then, running using:

docker build --build-arg user=$USER --build-arg uid=$(id -u) --build-arg gid=$(id -g) -t nautilus-ubuntu .

docker run -e DISPLAY=unix$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v /tmp/.docker.xauth:/tmp/.docker.xauth:rw -e XAUTHORITY=/tmp/.docker.xauth -t nautilus-ubuntu
like image 173
mangotango Avatar answered Jun 22 '26 02:06

mangotango



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!