Skip to content

Instantly share code, notes, and snippets.

@ahmedrowaihi
Created May 23, 2025 22:53
Show Gist options
  • Save ahmedrowaihi/94706630f3948a9efbfb9324c9c85244 to your computer and use it in GitHub Desktop.
Save ahmedrowaihi/94706630f3948a9efbfb9324c9c85244 to your computer and use it in GitHub Desktop.
Dockerfile for running Firefox in a VNC session with Playwright
# Use specific version for better reproducibility
FROM mcr.microsoft.com/playwright:v1.42.1
# Set build arguments for non-sensitive configurable values
ARG RESOLUTION=1920x1080
ARG VNC_PORT=5901
# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive \
DISPLAY=:1 \
RESOLUTION=${RESOLUTION} \
VNC_PORT=${VNC_PORT} \
USER=root
# Install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
tightvncserver \
dbus-x11 \
xfonts-base \
x11vnc \
openbox \
libpci3 \
adwaita-icon-theme \
gnome-themes-standard \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Create .vnc directory
RUN mkdir -p /root/.vnc
# Create Firefox profile directory
RUN mkdir -p /root/.mozilla/firefox/temp-profile && \
chmod 755 /root/.mozilla/firefox/temp-profile
# Create xstartup script
RUN <<EOF cat > /root/.vnc/xstartup
#!/bin/sh
set -x # Enable debug output
start_session() {
echo "Starting X session..."
# Basic X setup
xsetroot -solid grey
vncconfig -iconic &
echo "Starting window manager..."
openbox-session &
echo "Waiting for X to be ready..."
sleep 3
start_firefox
}
start_firefox() {
echo "Starting Firefox..."
# Try to find Firefox
FIREFOX_PATH=\$(find /ms-playwright -name firefox -type f | head -n 1)
echo "Found Firefox at: \$FIREFOX_PATH"
# Create a new temporary profile directory
TEMP_PROFILE=\$(mktemp -d)
echo "Created new temporary profile at: \$TEMP_PROFILE"
# Start Firefox with new temporary profile
\$FIREFOX_PATH --no-remote --new-instance --profile \$TEMP_PROFILE &
FIREFOX_PID=\$!
echo "Firefox started with PID \$FIREFOX_PID"
# Clean up old temporary profiles
find /tmp -maxdepth 1 -type d -name "tmp.*" -mmin +5 -exec rm -rf {} \; 2>/dev/null || true
}
# Function to handle cleanup
cleanup() {
echo "Cleaning up..."
kill \$FIREFOX_PID 2>/dev/null || true
killall openbox 2>/dev/null || true
vncserver -kill :1 || true
}
# Set up signal handling
trap cleanup SIGTERM SIGINT
# Start initial session
start_session
# Monitor both VNC and Firefox
while true; do
# Check VNC
if ! pgrep -f "Xtightvnc" > /dev/null; then
echo "VNC server stopped, restarting..."
cleanup
sleep 2
start_session
continue
fi
# Check Firefox
if ! kill -0 \$FIREFOX_PID 2>/dev/null; then
echo "Firefox stopped, restarting..."
kill \$FIREFOX_PID 2>/dev/null || true
sleep 2
start_firefox
fi
sleep 1
done
EOF
RUN chmod +x /root/.vnc/xstartup
# Create startup script
RUN <<EOF cat > /start.sh
#!/bin/bash
set -e
if [ -z "\$VNC_PASSWORD" ]; then
echo "Error: VNC_PASSWORD environment variable is not set"
exit 1
fi
echo "\$VNC_PASSWORD" | vncpasswd -f > /root/.vnc/passwd
chmod 600 /root/.vnc/passwd
echo "Starting VNC server at \$RESOLUTION..."
vncserver -kill :1 || true
vncserver -geometry \$RESOLUTION
# Keep container running
tail -f /dev/null
EOF
RUN chmod +x /start.sh
# Expose VNC port
EXPOSE ${VNC_PORT}
# Set entrypoint
ENTRYPOINT ["/start.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment