Px4 dockerfile with gazebo and Ros

I am trying to run a gazebo docker container. My host is using 440 drivers i am trying openGl and when I do make px4_sitl gazebo i get:

libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
X Error of failed request: GLXBadContext
Major opcode of failed request: 154 (GLX)
Minor opcode of failed request: 6 (X_GLXIsDirect)
Serial number of failed request: 38
Current serial number in output stream: 37
If I have opengl installed from docker file or not installed anything a tall

and

X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 154 (GLX)
Minor opcode of failed request: 3 (X_GLXCreateContext)
Value in failed request: 0x0
Serial number of failed request: 27
Current serial number in output stream: 28
If i run the same .run file I used to install drivers on my main machine

Here is my dockerfile ## PX4 ROS development environment#FROM px4io/px4-dev-simulation-bionic: - Pastebin.com
Also I have installed nvidia-docker and nvidia-smi shows my GC

I was facing similar issues with the video drivers. I’m assuming you were using the vanilla docker containers from px4.io

What worked for me was creating some ENV variables that expose the nvidia runtime drivers. Here is the modified Dockerfile:

FROM px4io/px4-dev-ros2-foxy

# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES \
    ${NVIDIA_VISIBLE_DEVICES:-all}
ENV NVIDIA_DRIVER_CAPABILITIES \
    ${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics

I tagged the image as “px4_foxy”.

Now, to launch the image run the following script:

#!/bin/bash
sudo xhost +si:localuser:root
XSOCK=/tmp/.X11-unix

XAUTH=/tmp/.docker.xauth
xauth_list=$(xauth nlist :0 | sed -e 's/^..../ffff/')
if [ ! -f $XAUTH ]
then
    echo XAUTH file does not exist. Creating one...
    touch $XAUTH
    chmod a+r $XAUTH
    if [ ! -z "$xauth_list" ]
    then
        echo $xauth_list | xauth -f $XAUTH nmerge -
    fi
fi

# Prevent executing "docker run" when xauth failed.
if [ ! -f $XAUTH ]
then
  echo "[$XAUTH] was not properly created. Exiting..."
  exit 1
fi

docker run --rm -it \
--env=LOCAL_USER_ID="$(id -u)" \
-v `pwd`/PX4-Autopilot:/src/PX4-Autopilot/:rw \
--env="XAUTHORITY=$XAUTH" \
--volume="$XAUTH:$XAUTH" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
--env="QT_X11_NO_MITSHM=1" \
--env="DISPLAY=$DISPLAY" \
--network host \
--privileged \
--gpus all
--name=px4-ros-foxy px4_foxy bash

sudo xhost -si:localuser:root

Tested in Ubuntu 22.04. Running gazebo with PX4 without issues.