welcom ! Handel home

2025年2月17日 星期一

ROS 2 Humble + Gazebo Fortress + X11 VNC + Python3(支援 rospy & roscpp)+ 預設登入用戶

 ROS 2 Humble + Gazebo Fortress + X11 VNC + Python3(支援 rospy & roscpp)+ 預設登入用戶

這個 Dockerfile 將支援:

  • Gazebo Fortress(最新 Gazebo 版本)
  • X11 forwarding(本機 GUI 支援)
  • VNC Server(遠端 GUI 存取)
  • Python3(支援 rospy
  • C++(支援 roscpp
  • 設定 x11vnc 預設用戶 ubuntu,密碼 ubuntu

🚀 Dockerfile

[dockerfile]

# 使用 ROS 2 Humble 官方映像 FROM ros:humble # 設定環境變數 ENV DEBIAN_FRONTEND=noninteractive # 更新系統並安裝基本工具 RUN apt update && apt install -y \ lsb-release wget gnupg2 curl software-properties-common mesa-utils \ x11vnc xvfb \ python3 python3-pip python3-colcon-common-extensions \ build-essential cmake git \ && rm -rf /var/lib/apt/lists/* # 設定 Gazebo 軟體庫 RUN sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable $(lsb_release -cs) main" > /etc/apt/sources.list.d/gazebo-stable.list' \ && wget https://packages.osrfoundation.org/gazebo.key -O - | apt-key add - # 安裝 Gazebo Fortress(Ignition Gazebo) RUN apt update && apt install -y \ gazebo-fortress \ ros-humble-ros-gz \ && rm -rf /var/lib/apt/lists/* # 安裝 rospy & roscpp 相關套件 RUN apt update && apt install -y \ ros-humble-rclpy ros-humble-rclcpp \ ros-humble-std-msgs ros-humble-sensor-msgs \ ros-humble-geometry-msgs ros-humble-nav-msgs \ && rm -rf /var/lib/apt/lists/* # 設定 Gazebo 環境變數 ENV GZ_SIM_RESOURCE_PATH=/usr/share/gz # 設定 ROS 2 環境變數 RUN echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc RUN echo "source /usr/share/gazebo/setup.sh" >> ~/.bashrc # 設定 Python 環境(rospy) RUN pip3 install -U pip setuptools RUN pip3 install empy numpy pyyaml rospkg # 建立 Ubuntu 使用者帳號 (ID: ubuntu / 密碼: ubuntu) RUN useradd -m -s /bin/bash ubuntu && echo "ubuntu:ubuntu" | chpasswd && adduser ubuntu sudo # 設定 VNC 環境變數 ENV DISPLAY=:1 ENV XDG_RUNTIME_DIR=/tmp/runtime-root # 設定 x11vnc 使用 Ubuntu 預設帳戶登入 RUN mkdir -p /home/ubuntu/.vnc && \ echo "ubuntu" | vncpasswd -f > /home/ubuntu/.vnc/passwd && \ chmod 600 /home/ubuntu/.vnc/passwd && \ chown -R ubuntu:ubuntu /home/ubuntu/.vnc # 建立 VNC 啟動指令(預設使用 ubuntu 用戶) RUN mkdir -p /home/ubuntu/.vnc && \ echo "#!/bin/bash" > /home/ubuntu/.vnc/start_vnc.sh && \ echo "Xvfb :1 -screen 0 1920x1080x16 &" >> /home/ubuntu/.vnc/start_vnc.sh && \ echo "x11vnc -display :1 -forever -usepw -rfbport 5900 -shared -noxrecord -noxfixes -noxdamage" >> /home/ubuntu/.vnc/start_vnc.sh && \ chmod +x /home/ubuntu/.vnc/start_vnc.sh && \ chown -R ubuntu:ubuntu /home/ubuntu/.vnc # 設定工作目錄 WORKDIR /home/ubuntu USER ubuntu # 預設執行指令:啟動 VNC Server 並開啟 Bash CMD ["bash", "-c", "/home/ubuntu/.vnc/start_vnc.sh & bash"]


🔹 建置 Docker 映像

bash
docker build -t ros2_humble_gazebo_vnc_user .

🔹 運行容器

1️⃣ 本機 X11 GUI 顯示

bash
xhost +local:docker docker run -it --rm --net=host \ -e DISPLAY=$DISPLAY \ -v /tmp/.X11-unix:/tmp/.X11-unix \ ros2_humble_gazebo_vnc_user

2️⃣ 遠端存取 VNC

bash
docker run -it --rm -p 5900:5900 ros2_humble_gazebo_vnc_user

然後使用 VNC Viewer 連接:

makefile
localhost:5900

帳號: ubuntu
密碼: ubuntu


🔹 測試 Gazebo

進入 Docker 容器後執行:

bash
gz sim shapes.sdf

🔹 測試 rospy

bash
ros2 run demo_nodes_py talker

🔹 測試 roscpp

bash
ros2 run demo_nodes_cpp talker

📌 結論

這個 Dockerfile 建立了一個完整的 ROS 2 + Gazebo + X11 VNC 環境,並支援:

  • Gazebo GUI 顯示(X11 & VNC)
  • Python (rospy)
  • C++ (roscpp)
  • VNC 遠端存取,帳號 ubuntu,密碼 ubuntu

🚀 現在你可以用 Ubuntu 預設用戶登入並開發 ROS 2 應用了! 🎉


沒有留言: