Files
RDC_Simulation/Dockerfile
2026-01-10 22:56:43 +00:00

202 lines
7.8 KiB
Docker

# =============================================================================
# RDC Simulation - Docker Build (GPU-enabled)
# =============================================================================
# Builds a complete ArduPilot + Gazebo simulation environment with GPU support.
# Your host system bashrc is NOT modified - everything stays in the container.
#
# Build:
# docker build -t rdc-simulation .
#
# Run (with GPU):
# docker run --gpus all -it --rm \
# -e DISPLAY=$DISPLAY \
# -v /tmp/.X11-unix:/tmp/.X11-unix \
# rdc-simulation
#
# Or use docker-compose:
# docker compose up
# =============================================================================
FROM ubuntu:24.04
# Avoid interactive prompts during build
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC
# =============================================================================
# SYSTEM DEPENDENCIES
# =============================================================================
RUN apt-get update && apt-get install -y \
# Build tools
curl \
wget \
git \
cmake \
build-essential \
gnupg \
lsb-release \
software-properties-common \
# Python
python3 \
python3-pip \
python3-venv \
python3-dev \
# Graphics / X11 / Vulkan
libgl1 \
libglx-mesa0 \
libgl1-mesa-dri \
mesa-utils \
x11-apps \
libxcb-xinerama0 \
libxkbcommon-x11-0 \
libxcb-cursor0 \
libxcb-icccm4 \
libxcb-keysyms1 \
libxcb-shape0 \
libvulkan1 \
# Video recording & codecs
ffmpeg \
xdotool \
wmctrl \
libx264-dev \
libx265-dev \
# Networking
netcat-openbsd \
# GStreamer (for ArduPilot Gazebo plugin video streaming)
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-good \
gstreamer1.0-libav \
gstreamer1.0-gl \
# OpenCV
libopencv-dev \
&& rm -rf /var/lib/apt/lists/*
# =============================================================================
# ROS 2 JAZZY
# =============================================================================
RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu noble main" > /etc/apt/sources.list.d/ros2.list \
&& apt-get update \
&& apt-get install -y \
ros-jazzy-ros-base \
ros-jazzy-geometry-msgs \
ros-jazzy-std-msgs \
ros-jazzy-nav-msgs \
ros-jazzy-sensor-msgs \
ros-jazzy-ros-gz \
&& rm -rf /var/lib/apt/lists/*
# =============================================================================
# GAZEBO HARMONIC + DEVELOPMENT PACKAGES
# =============================================================================
RUN wget https://packages.osrfoundation.org/gazebo.gpg -O /usr/share/keyrings/pkgs-osrf-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/pkgs-osrf-archive-keyring.gpg] http://packages.osrfoundation.org/gazebo/ubuntu-stable noble main" > /etc/apt/sources.list.d/gazebo-stable.list \
&& apt-get update \
&& apt-get install -y \
gz-harmonic \
# Development packages for building ardupilot_gazebo plugin
libgz-cmake3-dev \
libgz-sim8-dev \
libgz-plugin2-dev \
libgz-common5-dev \
libgz-physics7-dev \
libgz-sensors8-dev \
libgz-rendering8-dev \
libgz-transport13-dev \
libgz-msgs10-dev \
rapidjson-dev \
&& rm -rf /var/lib/apt/lists/*
# =============================================================================
# CREATE NON-ROOT USER
# =============================================================================
ARG USERNAME=pilot
ARG USER_UID=1000
ARG USER_GID=1000
# Create user - handle case where GID/UID 1000 may already exist
RUN apt-get update \
&& apt-get install -y sudo \
# Create group if it doesn't exist, or use existing
&& (getent group $USER_GID || groupadd --gid $USER_GID $USERNAME) \
# Create user if doesn't exist
&& (id -u $USER_UID >/dev/null 2>&1 || useradd --uid $USER_UID --gid $USER_GID -m $USERNAME) \
# Ensure home directory exists and has correct ownership
&& mkdir -p /home/$USERNAME \
&& chown $USER_UID:$USER_GID /home/$USERNAME \
# Add sudo permissions
&& echo "$USERNAME ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME \
&& rm -rf /var/lib/apt/lists/*
USER $USERNAME
WORKDIR /home/$USERNAME
# =============================================================================
# ARDUPILOT SITL
# =============================================================================
RUN git clone --recurse-submodules https://github.com/ArduPilot/ardupilot.git /home/$USERNAME/ardupilot
# Install ArduPilot prerequisites (writes to container's bashrc, not host)
WORKDIR /home/$USERNAME/ardupilot
RUN Tools/environment_install/install-prereqs-ubuntu.sh -y
# Build ArduCopter SITL
RUN . ~/.profile && ./waf configure --board sitl && ./waf copter
# =============================================================================
# ARDUPILOT GAZEBO PLUGIN
# =============================================================================
RUN git clone https://github.com/ArduPilot/ardupilot_gazebo.git /home/$USERNAME/ardupilot_gazebo
WORKDIR /home/$USERNAME/ardupilot_gazebo
RUN mkdir -p build && cd build \
&& cmake .. -DCMAKE_BUILD_TYPE=Release \
&& make -j$(nproc)
# =============================================================================
# PYTHON DEPENDENCIES (MAVProxy, pymavlink)
# =============================================================================
RUN pip3 install --user --break-system-packages pymavlink mavproxy pexpect pybullet numpy pillow opencv-python
# =============================================================================
# COPY PROJECT FILES
# =============================================================================
WORKDIR /home/$USERNAME/RDC_Simulation
COPY --chown=$USERNAME:$USERNAME . .
# Create venv and install requirements
RUN python3 -m venv venv \
&& . venv/bin/activate \
&& pip install --upgrade pip \
&& pip install -r requirements.txt
# =============================================================================
# ENVIRONMENT SETUP (container only - doesn't touch host)
# =============================================================================
ENV ARDUPILOT_HOME=/home/pilot/ardupilot
ENV PATH="${PATH}:/home/pilot/ardupilot/Tools/autotest:/home/pilot/.local/bin"
ENV GZ_SIM_SYSTEM_PLUGIN_PATH=/home/pilot/ardupilot_gazebo/build
ENV GZ_SIM_RESOURCE_PATH=/home/pilot/ardupilot_gazebo/models:/home/pilot/ardupilot_gazebo/worlds:/home/pilot/RDC_Simulation/gazebo/models
# Source ROS2 and ArduPilot env in bashrc (container's bashrc, not host)
RUN echo 'source /opt/ros/jazzy/setup.bash' >> ~/.bashrc \
&& echo 'source ~/.ardupilot_env 2>/dev/null || true' >> ~/.bashrc \
&& echo 'source ~/RDC_Simulation/venv/bin/activate' >> ~/.bashrc \
&& echo 'export ARDUPILOT_HOME=~/ardupilot' >> ~/.bashrc \
&& echo 'export PATH=$PATH:~/ardupilot/Tools/autotest:~/.local/bin' >> ~/.bashrc \
&& echo 'export GZ_SIM_SYSTEM_PLUGIN_PATH=~/ardupilot_gazebo/build' >> ~/.bashrc \
&& echo 'export GZ_SIM_RESOURCE_PATH=~/ardupilot_gazebo/models:~/ardupilot_gazebo/worlds:~/RDC_Simulation/gazebo/models' >> ~/.bashrc
# =============================================================================
# ENTRYPOINT
# =============================================================================
COPY --chown=$USERNAME:$USERNAME docker-entrypoint.sh /home/$USERNAME/docker-entrypoint.sh
RUN chmod +x /home/$USERNAME/docker-entrypoint.sh
WORKDIR /home/$USERNAME/RDC_Simulation
ENTRYPOINT ["/home/pilot/docker-entrypoint.sh"]
CMD ["bash"]