Skip to content

Commit e578f48

Browse files
itsbharatjstex2005
andauthored
Cross platform docker launch system and fix for GUI display issues on macOS with XQuartz for example 5 (#186)
- Fix macOS XQuartz display issues with dynamic DOCKER_DISPLAY variable - Add platform-specific setup instructions for macOS, Linux, Windows - Remove hard-coded IP addresses and display numbers - Add comprehensive troubleshooting for common X11 connection errors - Resolves Docker GUI display issues on macOS and improves cross-platform compatibility. --------- Co-authored-by: Stefano Dalla Gasperina <stefano.dallagasperina@austin.utexas.edu>
1 parent ddcfff1 commit e578f48

File tree

8 files changed

+479
-79
lines changed

8 files changed

+479
-79
lines changed

examples/5_docker_turtlesim/README.md

Lines changed: 165 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
# Example - TurtleSim in Docker
2-
For users who want to test the MCP server without needing to install ROS, this is an example that provides a dockerized ROS2 container preinstalled with the simplest possible 'robot' in the ROS ecosystem - TurtleSim.
2+
3+
For users who want to test the MCP server without needing to install ROS, this is an example that provides a dockerized ROS2 container preinstalled with the simplest possible 'robot' in the ROS ecosystem - TurtleSim.
34

45
Turtlesim is a lightweight simulator for learning ROS / ROS 2. It illustrates what ROS does at the most basic level to give you an idea of what you will do with a real robot or a robot simulation later on.
56

67
## Prerequisites
78

8-
**Note:** This tutorial is designed to be run on linux and has been tested with Ubuntu as well as [Ubuntu running on WSL](https://apps.microsoft.com/detail/9pn20msr04dw?hl=en-US&gl=US). Being a dockerized container, it is likely to work on other OS versions as well with the correct X11 forwarding settings.
9+
**Cross-Platform Support:** This tutorial works on Linux, macOS, and Windows with proper X11/display forwarding setup. Each platform has specific requirements detailed below.
910

1011
Before starting this tutorial, make sure you have the following installed:
1112

13+
### All Platforms
1214
- **Docker**: [Install Docker](https://docs.docker.com/get-docker/)
1315
- **Docker Compose**: Usually comes with Docker Desktop, or install separately
14-
- **X Server** (for Windows): Install [X410](https://x410.dev/) or another X Server from Microsoft Store
15-
- **X11 forwarding** (for Linux): `sudo apt-get install x11-apps`
16-
- **XQuartz** (for macOS): Install from [XQuartz website](https://www.xquartz.org/)
1716

18-
**Note:** If your OS is Windows, take a look at the following:
17+
### Platform-Specific Requirements
18+
19+
#### macOS
20+
- **XQuartz**: Install from [XQuartz website](https://www.xquartz.org/)
21+
- **Important**: XQuartz setup can be complex - see [macOS Setup](#macos-setup) section below for detailed instructions
22+
23+
#### Linux
24+
- **X11 forwarding**: `sudo apt-get install x11-apps`
25+
- Usually works out of the box with minimal setup
26+
27+
#### Windows
28+
- **X Server**: Install [X410](https://x410.dev/), [VcXsrv](https://sourceforge.net/projects/vcxsrv/), or another X Server from Microsoft Store
29+
- **WSL recommended**: Works best with Windows Subsystem for Linux
30+
1931
<details>
2032
<summary><strong>PowerShell and WSL (Windows)</strong></summary>
2133

@@ -36,23 +48,38 @@ cd examples/5_docker_turtlesim
3648
docker compose build --no-cache turtlesim
3749
```
3850

39-
### 2. Start the Container
51+
### 2. Launch Turtlesim
52+
53+
#### Automatic Setup (Recommended)
4054

41-
Launch the turtlesim container
55+
The easiest way to launch turtlesim with proper X11 setup:
4256

4357
```bash
44-
docker compose up
58+
./launch.sh
4559
```
4660

47-
If your OS is Windows and you want to launch docker in PowerShell, you first need to set the DISPLAY:
61+
This script automatically detects your OS and handles all platform-specific X11 configuration. It will:
62+
- **macOS**: Start XQuartz, detect display, configure IP-based forwarding
63+
- **Linux**: Set up X11 permissions with `xhost +local:docker`
64+
- **Windows**: Configure X server connection via `host.docker.internal`
65+
66+
#### Manual Setup (Advanced)
67+
68+
If you prefer manual control or the automatic script doesn't work:
69+
70+
**macOS:**
4871
```bash
49-
$env:DISPLAY="host.docker.internal:0.0"
72+
./docker/scripts/launch_macos.sh
5073
```
5174

52-
If your OS is Ubuntu/WSL and the docker doesn't run successfully, consider:
75+
**Linux (or Windows WSL):**
76+
```bash
77+
./docker/scripts/launch_linux.sh
78+
```
5379

80+
**Windows:**
5481
```bash
55-
dos2unix docker/scripts/launch_turtlesim.sh
82+
./docker/scripts/launch_windows.sh
5683
```
5784

5885
The container will automatically start both turtlesim and rosbridge websocket server. You should see:
@@ -69,16 +96,16 @@ Launch the container in the background:
6996
```bash
7097
docker compose up -d
7198
```
72-
And attach to the container
7399

100+
And attach to the container:
74101
```bash
75102
docker exec -it ros2-turtlesim bash
76103
```
77104

78105
Once inside the container, you can manually launch turtle teleop to control the turtle:
79106

80107
```bash
81-
source /opt/ros/humble/setup.bash
108+
source /opt/ros/${ROS_DISTRO}$/setup.bash
82109
ros2 run turtlesim turtle_teleop_key
83110
```
84111

@@ -87,45 +114,145 @@ This will allow you to use arrow keys or WASD to manually move the turtle around
87114
## Integration with MCP Server
88115

89116
Once turtlesim and rosbridge are running, you can connect the MCP server to control the turtle programmatically.
90-
Follow the [installation guide](../../docs/installation.md) for full setup instructions if you haven't already set up the MCP server.
117+
Follow the [installation guide](../../docs/installation.md) for full setup instructions if you haven't already set up the MCP server.
91118

92-
Since it is running on the same machine, you can tell the LLM to connect to the robot on localhost.
119+
Since it is running on the same machine, you can tell the LLM to connect to the robot on localhost.
93120

121+
## Platform-Specific Setup
94122

95-
## Troubleshooting
123+
<details>
124+
<summary><strong>macOS Setup</strong></summary>
96125

97-
### Display Issues (Linux)
126+
macOS requires special X11 forwarding setup. Follow these steps carefully:
98127

99-
If you encounter display issues on Linux, run:
128+
### Step 1: Install XQuartz
129+
Download and install from [XQuartz website](https://www.xquartz.org/)
100130

131+
### Step 2: Configure XQuartz
132+
1. **Start XQuartz**: `open -a XQuartz`
133+
2. **Wait for it to fully load** (you'll see an xterm window)
134+
135+
### Step 3: Detect Your Setup
101136
```bash
102-
xhost +local:docker
137+
# Check if XQuartz is running and which display it's using
138+
ps aux | grep -i xquartz
139+
140+
# You should see something like:
141+
# /opt/X11/bin/Xquartz :1 -listen tcp ...
142+
# The `:1` or `:0` is your display number
143+
```
144+
145+
### Step 4: Get Your Machine IP
146+
```bash
147+
# Get your machine's IP address
148+
ifconfig en0 | grep inet | awk '$1=="inet" {print $2}'
149+
# Example output: 10.1.56.72
150+
```
151+
152+
### Step 5: Set Up Environment
153+
```bash
154+
# Set DISPLAY for your Mac (use the display number from Step 3)
155+
export DISPLAY=:1 # or :0 depending on what you found
156+
157+
# Allow X11 connections
158+
xhost +
159+
160+
# Set DISPLAY for Docker (use your IP from Step 4 + display number)
161+
export DOCKER_DISPLAY=<YOUR_IP> # Replace with your actual IP
103162
```
163+
</details>
164+
165+
## Troubleshooting
104166

105-
### Display Issues (macOS)
167+
### macOS Display Issues
106168

107-
For macOS users, make sure XQuartz is running and configured:
169+
<details>
170+
<summary><strong>Problem: <code>qt.qpa.xcb: could not connect to display</code></strong></summary>
171+
172+
**Solutions**:
173+
174+
1. **Check XQuartz is running**:
175+
```bash
176+
ps aux | grep X11
177+
```
178+
179+
2. **Verify display number**:
180+
```bash
181+
# Look for :0 or :1 in the Xquartz process
182+
ps aux | grep Xquartz | grep -o ":[0-9]"
183+
```
184+
185+
3. **Check your IP address**:
186+
```bash
187+
ifconfig en0 | grep inet
188+
```
189+
190+
4. **Set correct DOCKER_DISPLAY**:
191+
```bash
192+
export DOCKER_DISPLAY="YOUR_IP:DISPLAY_NUMBER"
193+
# Example: export DOCKER_DISPLAY="10.1.56.72:1"
194+
```
195+
196+
5. **Allow X11 connections**:
197+
```bash
198+
export DISPLAY=:1 # Use your display number
199+
xhost +
200+
```
201+
</details>
202+
203+
<details>
204+
<summary><strong>Problem: <code>xhost: unable to open display ":0"</code></strong></summary>
205+
206+
**Solution**: XQuartz might be using `:1` instead of `:0`:
108207

109208
```bash
110-
# Start XQuartz
111-
open -a XQuartz
209+
export DISPLAY=:1
210+
xhost +
211+
```
212+
</details>
112213

113-
# Allow connections from localhost
114-
xhost +localhost
214+
### Linux Display Issues
215+
216+
<details>
217+
<summary><strong>Display issues on Linux</strong></summary>
218+
219+
If you encounter display issues on Linux, run:
220+
221+
```bash
222+
xhost +local:docker
115223
```
224+
</details>
225+
226+
### Windows Display Issues
227+
228+
<details>
229+
<summary><strong>Display issues on Windows</strong></summary>
116230

117-
### Display Issues (Windows/PowerShell)
118231
For Windows users, make sure you install an X Server (X410) and set the DISPLAY:
119232

120233
```bash
121-
$env:DISPLAY="host.docker.internal:0.0"
234+
$env:DOCKER_DISPLAY="host.docker.internal:0.0"
122235
```
236+
</details>
237+
238+
### General Issues
239+
240+
<details>
241+
<summary><strong>Problem: Container starts but no window appears</strong></summary>
123242

124-
### Container Networking
243+
**Solutions**:
244+
1. Check if the window is hidden behind other windows
245+
2. Look in Mission Control (macOS) or Alt+Tab (Windows/Linux)
246+
3. Verify your DOCKER_DISPLAY is set correctly for your platform
247+
</details>
125248

126-
If you need to access the container from outside, the container uses host networking mode, so ROS2 topics should be accessible on localhost.
249+
<details>
250+
<summary><strong>Problem: <code>libGL error: No matching fbConfigs or visuals found</code></strong></summary>
127251

128-
### Manual Launch (Alternative)
252+
**Solution**: This is just a warning and doesn't prevent the GUI from working. The turtlesim window should still appear.
253+
</details>
254+
255+
## Manual Launch (Alternative)
129256

130257
If the automatic launch isn't working or you prefer to launch turtlesim manually, you can run these commands inside the container:
131258

@@ -134,7 +261,7 @@ If the automatic launch isn't working or you prefer to launch turtlesim manually
134261
docker exec -it ros2-turtlesim bash
135262

136263
# Source ROS2 environment
137-
source /opt/ros/humble/setup.bash
264+
source /opt/ros/${ROS_DISTRO}$/setup.bash
138265

139266
# Start turtlesim in one terminal
140267
ros2 run turtlesim turtlesim_node
@@ -143,11 +270,11 @@ ros2 run turtlesim turtlesim_node
143270
ros2 run turtlesim turtle_teleop_key
144271
```
145272

146-
### Testing Turtlesim
273+
## Testing Turtlesim
147274

148275
If you need to verify that turtlesim is working correctly:
149276

150-
#### ROS2 Topic Inspection
277+
### ROS2 Topic Inspection
151278

152279
In a separate terminal, you can inspect the ROS2 topics:
153280

@@ -156,7 +283,7 @@ In a separate terminal, you can inspect the ROS2 topics:
156283
docker exec -it ros2-turtlesim bash
157284

158285
# Source ROS2 environment
159-
source /opt/ros/humble/setup.bash
286+
source /opt/ros/${ROS_DISTRO}$/setup.bash
160287

161288
# List all topics
162289
ros2 topic list
@@ -168,7 +295,7 @@ ros2 topic echo /turtle1/pose
168295
ros2 topic echo /turtle1/cmd_vel
169296
```
170297

171-
#### ROS Bridge WebSocket Server
298+
### ROS Bridge WebSocket Server
172299

173300
The rosbridge websocket server is automatically started and available at `ws://localhost:9090`. You can test the connection using a WebSocket client or the MCP server.
174301

@@ -196,10 +323,8 @@ docker rmi ros-mcp-server_turtlesim
196323

197324
Now that you have turtlesim running, you can:
198325

199-
200326
1. **Try more complex commands** like drawing shapes or following paths
201327
2. **Install ROS Locally** to add more nodes and services
202328
3. **Explore other examples in this repository**
203329

204-
205-
This example provides a foundation for understanding how the MCP server can interact with ROS2 systems, from simple simulators like turtlesim to complex robotic platforms.
330+
This example provides a foundation for understanding how the MCP server can interact with ROS2 systems, from simple simulators like turtlesim to complex robotic platforms.

examples/5_docker_turtlesim/docker-compose.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ services:
77
environment:
88
- DISPLAY=${DISPLAY}
99
- ROS_DISTRO=humble
10+
- QT_X11_NO_MITSHM=1
1011
volumes:
12+
# Mount launch file into the container
13+
- ./launch_turtlesim.launch.py:/ros2_ws/launch/launch_turtlesim.launch.py:ro
14+
# Linux X11 socket (only works on Linux)
1115
- /tmp/.X11-unix:/tmp/.X11-unix:rw
12-
- ./docker/scripts:/ros2_ws/scripts:ro
13-
network_mode: host
16+
ports:
17+
- "9090:9090"
1418
stdin_open: true
1519
tty: true
16-
command: bash -c "/ros2_ws/scripts/launch_turtlesim.sh"
20+
command: ["ros2", "launch", "/ros2_ws/launch/launch_turtlesim.launch.py"]

examples/5_docker_turtlesim/docker/scripts/launch_turtlesim.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)