Skip to content

X11 Docker Images

Create docker images for X11 GUI applications. base on ubunu:22.04

Dockerfile
dockerfile
# Use Ubuntu 22.04 as the base image
FROM ubuntu:22.04

# Set non-interactive mode for apt-get to avoid prompts
ENV DEBIAN_FRONTEND=noninteractive

# Update package list and install X11 and OpenGL dependencies
RUN apt-get update && apt-get install -y \
    # Core X11 libraries
    libx11-6 \
    libxext6 \
    libxrender1 \
    libxinerama1 \
    libxcursor1 \
    libxrandr2 \
    libxi6 \
    # OpenGL/Mesa for rendering
    libgl1-mesa-glx \
    libgl1-mesa-dri \
    libglu1-mesa \
    # Utilities for X11 auth and testing
    xauth \
    mesa-utils \
    # apps
    cheese \
    gstreamer1.0-alsa \
    gstreamer1.0-libav \
    gstreamer1.0-plugins-bad \
    gstreamer1.0-plugins-base \
    gstreamer1.0-plugins-good \
    gstreamer1.0-plugins-ugly \
    gstreamer1.0-tools \
    # Clean up
    && rm -rf /var/lib/apt/lists/*

# Set up environment variables for X11
ENV DISPLAY=:0
# Optional: Define a default runtime directory (can be overridden at runtime)
ENV XDG_RUNTIME_DIR=/tmp/runtime-root

# Create a non-root user (optional, for better security and matching host permissions)
ARG USERNAME=user
ARG UID=1000
ARG GID=1000

# add new sudo user
RUN useradd -m $USERNAME && \
        echo "$USERNAME:$USERNAME" | chpasswd && \
        usermod --shell /bin/bash $USERNAME && \
        usermod -aG sudo $USERNAME && \
        usermod -aG video $USERNAME && \
        usermod -aG dialout $USERNAME && \
        mkdir /etc/sudoers.d && \
        echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/$USERNAME && \
        chmod 0440 /etc/sudoers.d/$USERNAME && \
        usermod  --uid $UID $USERNAME && \
        groupmod --gid $GID $USERNAME

# Switch to the non-root user
# USER $USERNAME

# Set working directory
WORKDIR /home/$USERNAME

# Default command (can be overridden)
CMD ["bash"]
build
docker build -t ubuntu/22.04:gui -f Dockerfile .

The command xhost +local:docker is used on Linux systems to manage access control for the X11 display server. Allows any process running locally as part of the "docker" context (Docker containers) to connect to your X server.

xhost +local:docker

check x11 app

glxgears is a simple OpenGL application that draws a rotating set of gears. It is often used to test the performance of OpenGL on a system.

run
1
2
3
4
5
docker run -it --rm \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    ubuntu/22.04:gui \
    glxgears

Share camera with docker

Simple

Device must be connected before running the container.

run
docker run -it --rm \
    --user user \
    --hostname gui \
    --device /dev/video0:/dev/video0 \
    -e DISPLAY=$DISPLAY \
    --user user \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -v /run/user/1000:/run/user/1000 \
    ubuntu/22.04:gui \
    /bin/bash
1
2
3
4
5
# Test
gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink

# v4l
gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! autovideosink

Advanced

1
2
3
ls -l /dev/video*
#
crw-rw----+ 1 root video 81, 0 Apr  5 16:57 /dev/video0
run
docker run -it --rm \
    --user user \
    --hostname gui \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -v /dev/dri:/dev/dri \
    --device-cgroup-rule='c 81:* rwm' \
    -v /dev:/dev \
    ubuntu/22.04:gui \
    /bin/bash

Advanced

  • --device-cgroup-rule: allows the container to interact with any video device (major number 81) created after startup.
  • -v /dev:/dev: ensures the container sees the host’s /dev directory in real-time.host.