Mac: Run ubuntu command with GUI in docker
2025-07-01
Step 1: create Dockerfile
FROM ubuntu:22.04
# Install xclock and necessary X11 libraries
RUN apt-get update && apt-get install -y \
x11-apps \
&& rm -rf /var/lib/apt/lists/*
# Set the DISPLAY environment variable
ENV DISPLAY=host.docker.internal:0
# Run xclock
CMD ["xclock"]
Step 2: Build the image
docker build -t xclock-ubuntu .
Step 3: Set XQuartz and restart it

Step 4: Run "xhost + 127.0.0.1", and this "xhost +localhost" wont work
Step 5: Run
docker run --rm \
-e DISPLAY=host.docker.internal:0 \
xclock-ubuntu

Another way
docker run --name test -it ubuntu
apt-get update
apt-get install x11-apps
export DISPLAY=host.docker.internal:0
xclock