ROS 2 Humble Setup in Docker
Prerequisites
To follow this tutorial, you need:
- Docker installed
- Docker Compose installed
- Intel hardware with access to
/dev
- X11 installed on your machine for GUI forwarding (Gazebo)
Step 1: Create the Dockerfile
Create a file named dockerfile.dev
with the following contents:
FROM osrf/ros:humble-desktop AS base
ARG USERNAME=ROD
ARG USER_UID=1000
ARG USER_GID=$USER_UID
ARG DEBIAN_FRONTEND=noninteractive
# Set default shell to bash
SHELL ["/bin/bash", "-c"]
# Graphics and compilation packages and env for dev only
# Set up X11
ENV export DISPLAY=:0.0
ENV export QT_QUICK_BACKEND=software
ENV QT_X11_NO_MITSHM 1
ENV PATH /usr/lib/ccache:$QT_DESKTOP/bin:$PATH
RUN apt-get update && apt-get -y install libnss3 \
libxcomposite1 libxdamage1 libxrandr2 libxtst6 \
&& rm -rf /var/lib/apt/lists/*
# libxss1 libasound2 libatk-bridge2.0-0 libatspi2.0-0 libvpx5 libevent-2.1-6 libicu60 -y
RUN apt-get update && apt-get -y --quiet --no-install-recommends install \
apt-utils \
binutils \
build-essential \
ca-certificates \
ccache \
checkinstall \
cmake \
curl \
espeak \
fuse \
g++ \
gcc \
git \
gosu \
kmod \
libespeak-dev \
libfontconfig1 \
libfuse2 \
libgstreamer-plugins-base1.0-dev \
libgstreamer1.0-0 \
libgstreamer1.0-dev \
libsdl2-dev \
libssl-dev \
libudev-dev \
locales \
make \
ninja-build \
openssh-client \
openssl \
patchelf \
pkg-config \
rsync \
speech-dispatcher \
wget \
xvfb \
zlib1g-dev \
&& apt-get -y autoremove \
&& apt-get clean autoclean \
&& rm -rf /var/lib/apt/lists/{apt,dpkg,cache,log} /tmp/* /var/tmp/*
RUN apt-get update && apt-get install -y \
'^libxcb.*-dev' libx11-xcb-dev \
libglu1-mesa-dev libxrender-dev \
libxi-dev libxkbcommon-dev libxkbcommon-x11-dev \
libxkbcommon-x11-0 libxcb-xinerama0 libxcb-* \
libxcb-xinerama0-dev libxkbcommon-x11-dev \
&& rm -rf /var/lib/apt/lists/*
# Install necessary packages for hardware access
RUN apt-get update && apt-get install -y \
apt-utils \
build-essential \
ros-humble-gazebo-* \
ros-humble-micro-ros-msgs \
cmake \
python3-serial \
python3-pip \
libusb-1.0-0-dev \
libxml2-dev \
libxslt-dev \
&& rm -rf /var/lib/apt/lists/*
RUN groupadd --gid "$USER_GID" "$USERNAME" \
&& useradd -s /bin/bash --uid "$USER_UID" --gid "$USER_GID" -m "$USERNAME" \
&& echo "$USERNAME" ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/"$USERNAME" \
&& chmod 0440 /etc/sudoers.d/"$USERNAME" \
&& echo "\n# Added by ROD Dockerfile:" >> /home/"$USERNAME"/.bashrc \
&& echo "source /usr/share/bash-completion/completions/git" >> /home/"$USERNAME"/.bashrc
RUN usermod -a -G dialout "$USERNAME"
# Switch to our new user
USER $USERNAME
ENV USER=$USERNAME
# Create a workspace directory
RUN mkdir -p /home/"$USERNAME"/ros2_ws/src
RUN source /opt/ros/humble/setup.bash
RUN echo "source /opt/ros/humble/setup.bash" >> /home/"$USERNAME"/.bashrc
RUN echo "source /home/ROD/ros2_ws/install/setup.bash" >> /home/"$USERNAME"/.bashrc
# Copy your ROS 2 packages into the container
#COPY ./src /$USERNAME/ros2_ws/src
# Install dependencies and build the ROS 2 workspace
WORKDIR /home/$USERNAME/ros2_ws
RUN source /opt/ros/humble/setup.bash && \
rosdep update && \
rosdep install --from-paths src --ignore-src -y && \
colcon build --symlink-install
RUN source /opt/ros/humble/setup.bash
RUN source /home/ROD/ros2_ws/install/setup.bash
Step 2: Create Docker Compose File
Create a file named docker-compose_dev.yml
with the following contents:
services:
ros2_dev:
container_name: rod
build:
context: .
dockerfile: Dockerfile.dev
privileged: true
network_mode: "host"
tty: true
environment:
- DISPLAY=$DISPLAY
user: 1000:1000
# command to get the right group number: getent group render | cut -d: -f3
group_add:
- "${RENDER_GROUP_ID}"
volumes:
- /tmp/.X11-unix:/tmp/.X11-unix
- /dev:/dev
- /sys:/sys
- /etc/udev:/etc/udev
- ./src:/home/ros2-on-intel/ros2_ws/src
- ./build:/home/ros2-on-intel/ros2_ws/build
- ./install:/home/ros2-on-intel/ros2_ws/install
- ./log:/home/ros2-on-intel/ros2_ws/log
devices:
- /dev/dri/renderD128:/dev/dri/renderD128
command: bash -c " source /opt/ros/humble/setup.bash && /bin/bash "
Step 3: Script to Launch Docker
Create a script named start_ros2_dev.sh
to automate launching the container:
#!/bin/bash
# Enable X11 access
xhost +local:docker
# Launch the Docker container
docker-compose -f docker-compose_dev.yml up --build
Make the script executable:
chmod +x start_ros2_dev.sh
Step 4: Building and Running the Container
-
Build the container:
Run the script./start_ros2_dev.sh
to build and run the container. It will automatically build the Docker image and start the container with access to your system’s/dev
and X11 forwarding enabled. -
Access the container:
docker exec -it {docker container name} /bin/bash
Once inside the container, your ROS 2 Humble environment is fully set up. You can now run ROS 2 nodes, work with Gazebo, and use any hardware connected to /dev
.
Step 5: Testing Gazebo
- Launch Gazebo to test if X11 forwarding works:
gazebo
- If everything is set up correctly, Gazebo should open in your host’s GUI.