How to run GUI app inside Docker container
$ tree
.
├── docker_build.sh
├── docker_run.sh
└── image
└── Dockerfile
[image/Dockerfile]
# Start with your favorite linux distro.
# I will use "Fedora".
FROM fedora:40
# Setup your GUI application that you want to use
# I will use "xclock".
RUN dnf install --assumeyes xclock
# Install one of the many X11 servers
# I will use "Xorg X server".
RUN dnf install --assumeyes xorg-x11-server-Xorg
# Setup the same user in the container that you are logged in
# on your host machine. This is very important step. Without it
# you will get authorization problems while connecting to display.
ARG USER_NAME USER_ID GROUP_NAME GROUP_ID
RUN groupadd --gid ${GROUP_ID} ${GROUP_NAME}
RUN useradd --uid ${USER_ID} --gid ${GROUP_ID} ${USER_NAME}
# Switch to the user created in previous step.
USER ${USER_NAME}
WORKDIR /home/${USER_NAME}
[docker_build.sh]
#!/usr/bin/env bash
# Build image from Dockerfile.
# Pass logged in user info into build args.
docker build \
--build-arg USER_NAME=$(id --user --name) \
--build-arg USER_ID=$(id --user) \
--build-arg GROUP_NAME=$(id --group --name) \
--build-arg GROUP_ID=$(id --group) \
--tag gui-demo:latest image \
$1
[docker_run.sh]
#!/usr/bin/env bash
# Run container from previously built image.
# You need to add two mandatory options:
# "--network host" and "--env DISPLAY"
# This is the simplest way that I know to connect X11 server
# inside container to your host machine's X11 display.
docker run --rm -it \
--network host \
--env DISPLAY \
gui-demo:latest \
$1
