You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS(M1 Pro)Docker运行ROS2的rviz2 GUI失败求助

在M1 Pro Mac的Docker容器中运行RViz2的解决方案

问题背景

使用搭载M1 Pro芯片的MacBook Pro,通过Docker Desktop构建了基于Ubuntu 22.04的ROS2 Humble镜像,容器内ROS2基础功能正常,但执行rviz2时出现Qt平台插件报错:

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.

Aborted

解决步骤

1. 在Mac上安装并配置XQuartz

Mac本身无内置X服务器,需通过XQuartz提供图形界面转发能力:

  • 通过Homebrew安装:brew install --cask xquartz,或直接下载官方安装包完成安装
  • 安装后必须重启Mac才能生效
  • 打开XQuartz,进入「偏好设置 -> 安全」,勾选「允许来自网络的客户端」
  • 打开终端执行xhost +,允许所有客户端连接X服务器(测试场景使用,生产环境可限制特定IP)

2. 获取Mac本地IP地址

执行以下命令找到当前网络的IP(通常为en0接口的inet地址):

ifconfig | grep inet

记录输出中类似inet 192.168.x.x的地址。

3. 调整容器运行命令,添加X11转发参数

运行容器时需传递DISPLAY环境变量、挂载X11套接字,同时适配ARM架构:

docker run -it \
  --env DISPLAY=<你的Mac本地IP>:0 \
  --volume /tmp/.X11-unix:/tmp/.X11-unix \
  --privileged \
  <你的镜像名称> /bin/bash

替换<你的Mac本地IP>为步骤2获取的地址,<你的镜像名称>为构建好的ROS2镜像名。

4. 完善Dockerfile,补充Qt依赖(可选但推荐)

在Dockerfile的# install common packages区块后添加Qt相关依赖安装命令,避免缺失库导致插件加载失败:

# 安装RViz2所需的Qt依赖
RUN apt install -y \
    libxcb-xinerama0 \
    libxcb-cursor0 \
    libxkbcommon-x11-0 \
    libxcb-icccm4 \
    libxcb-image0 \
    libxcb-keysyms1 \
    libxcb-randr0 \
    libxcb-render-util0 \
    libxcb-shape0 \
    libxcb-xkb1

重新构建镜像即可。

验证

进入容器后执行rviz2,Mac上应能正常弹出RViz2图形界面。


内容的提问来源于stack exchange,提问作者Hussein Fawzy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 10:20:40