Files
RDC_Simulation/docs/docker.md
2026-01-10 22:56:43 +00:00

356 lines
7.6 KiB
Markdown

# Docker Setup for RDC Simulation
This guide explains how to run the RDC Simulation environment inside a Docker container with NVIDIA GPU support and X11 display forwarding. **This is the recommended approach** for Ubuntu 25.x+ or any system where native installation has dependency issues.
## Benefits of Docker
| Aspect | Native Install | Docker Container |
|--------|---------------|------------------|
| **Ubuntu 25.x support** | ❌ Dependency conflicts | ✅ Works perfectly |
| **ROS 2 Jazzy** | ❌ No packages for 25.x | ✅ Full support |
| **Gazebo dev packages** | ❌ Library conflicts | ✅ All packages work |
| **Host system** | Modified (packages, bashrc) | ✅ Untouched |
| **Reproducibility** | Varies by system | ✅ Identical everywhere |
## Prerequisites
### 1. Docker Engine
```bash
# Install Docker
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
newgrp docker # or log out and back in
```
### 2. NVIDIA GPU & Drivers (Required for Gazebo)
```bash
# Check your GPU is detected
nvidia-smi
```
### 3. NVIDIA Container Toolkit
```bash
# Install NVIDIA Container Toolkit
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
```
Verify GPU is accessible in Docker:
```bash
docker run --rm --gpus all nvidia/cuda:12.0-base nvidia-smi
```
---
## Quick Start
### 1. Build the Image
```bash
cd ~/RDC_Simulation
docker compose build
```
> **Note:** First build takes 20-40 minutes (downloads ~5GB, compiles ArduPilot + plugins).
### 2. Allow X11 Display Access
```bash
xhost +local:docker
```
> Add this to your `~/.bashrc` to make it permanent.
### 3. Run the Container
```bash
docker compose run --rm simulation
```
This drops you into a bash shell inside the container as user `pilot`.
---
## Running the Simulation
Once inside the container, the environment is pre-configured. You'll see a welcome message with available commands.
### Option 1: All-in-One Script
```bash
./scripts/run_ardupilot_controller.sh
```
### Option 2: Multi-Terminal (Recommended for Development)
**Terminal 1 - Start Gazebo:**
```bash
./scripts/run_ardupilot_sim.sh runway
```
**Terminal 2 - Open new shell in same container:**
```bash
# On host, find container and exec into it
docker exec -it rdc-sim bash
# Then start SITL
sim_vehicle.py -v ArduCopter -f gazebo-iris --model JSON --console
```
**Terminal 3 - Open another shell:**
```bash
docker exec -it rdc-sim bash
# Run controller
python scripts/run_ardupilot.py --pattern square
```
---
## Docker Commands Reference
### Build Commands
```bash
# Build image (standard)
docker compose build
# Build with no cache (clean rebuild)
docker compose build --no-cache
# Build just the Dockerfile directly
docker build -t rdc-simulation .
```
### Run Commands
```bash
# Interactive shell
docker compose run --rm simulation
# Run specific command
docker compose run --rm simulation ./scripts/run_ardupilot_sim.sh runway
# Run headless (no display, for CI)
docker compose run --rm simulation-headless
# Start container in background
docker compose up -d simulation
docker exec -it rdc-sim bash
```
### Cleanup Commands
```bash
# Stop all running containers
docker compose down
# Stop and remove containers
docker compose down --remove-orphans
# Remove built image (to rebuild from scratch)
docker rmi rdc-simulation:latest
# Remove all project containers and images
docker compose down --rmi all
# Full cleanup (removes image + build cache)
docker compose down --rmi all
docker builder prune -f
```
### Complete Uninstall & Rebuild
To completely remove everything and start fresh:
```bash
# 1. Stop any running containers
docker compose down
# 2. Remove the image
docker rmi rdc-simulation:latest
# 3. Clear Docker build cache (optional, saves disk space)
docker builder prune -f
# 4. Clear unused Docker data (optional, aggressive cleanup)
docker system prune -f
# 5. Rebuild from scratch
docker compose build --no-cache
```
---
## Video Recording
The Docker image includes `ffmpeg` and tools to record the simulation.
### Record Flight
```bash
python scripts/record_flight.py --pattern square --quality high --output my_flight
```
Videos are saved to `recordings/` inside the container.
### Copy Recordings to Host
```bash
# From host machine
docker cp rdc-sim:/home/pilot/RDC_Simulation/recordings .
```
---
## Headless Mode (CI/Server)
For running on servers without a display:
```bash
docker compose run --rm simulation-headless
```
Gazebo can still render offscreen for video recording using software rendering.
---
## Configuration Reference
### docker-compose.yml Services
| Service | Use Case |
|---------|----------|
| `simulation` | Full GUI with X11 display |
| `simulation-headless` | No display, for CI/testing |
### Environment Variables
| Variable | Purpose |
|----------|---------|
| `DISPLAY` | X11 display (auto-detected) |
| `NVIDIA_VISIBLE_DEVICES` | GPU visibility |
| `HEADLESS` | Set to `1` for software rendering |
### Volumes
| Mount | Purpose |
|-------|---------|
| `/tmp/.X11-unix` | X11 socket for display |
| `~/.Xauthority` | X11 authentication |
---
## Troubleshooting
### "permission denied" on docker commands
```bash
sudo usermod -aG docker $USER
newgrp docker # or log out and back in
```
### "Could not select device driver 'nvidia'"
NVIDIA Container Toolkit not installed or configured:
```bash
# Reinstall toolkit
sudo apt-get install -y nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
```
### "All CUDA-capable devices are busy"
```bash
# Check GPU on host
nvidia-smi
# Make sure no other process is using all GPU memory
```
### "Could not connect to display :0"
```bash
# On host machine, allow X11 connections
xhost +local:docker
# If using SSH, ensure X11 forwarding is enabled
ssh -X user@host
```
### "Error response from daemon: No such image"
Build the image first:
```bash
docker compose build
```
### Build fails at ArduPilot step
The ArduPilot build requires significant memory. If it fails:
```bash
# Increase Docker memory limit (Docker Desktop)
# Or reduce parallel jobs in Dockerfile:
# Change: make -j$(nproc)
# To: make -j2
```
### Container runs but Gazebo crashes
```bash
# Check OpenGL inside container
docker compose run --rm simulation glxinfo | grep "OpenGL renderer"
# Should show your NVIDIA GPU, not "llvmpipe" (software)
```
---
## Development Workflow
### Edit Code on Host
Your code changes in `~/RDC_Simulation` are **not** automatically reflected in the container (unless you mount the volume). To test changes:
```bash
# Option 1: Rebuild image (slow)
docker compose build
# Option 2: Mount your code (edit docker-compose.yml to add volume)
volumes:
- .:/home/pilot/RDC_Simulation
```
### Build Custom Image Tag
```bash
docker build -t rdc-simulation:dev .
docker run --gpus all -it rdc-simulation:dev
```
---
## What's Installed in the Container
| Component | Version |
|-----------|---------|
| Ubuntu | 24.04 LTS |
| ROS 2 | Jazzy |
| Gazebo | Harmonic |
| Python | 3.12 |
| ArduPilot SITL | Latest (git) |
| ArduPilot Gazebo Plugin | Latest (git) |
| MAVProxy | Latest (pip) |
All Gazebo development packages are included for plugin compilation.