Below is a “minimal-but-practical” recipe to get a ROS 2 development container up and running quickly.
It starts from scratch (Ubuntu 22.04 + ROS 2 Humble) but you can adapt it to any ROS 2 distro by just changing a couple lines.
Feel free to copy-paste the snippets—everything is ready to run.
(If you only need a turnkey image and don’t care about customisation, jump to “The 30-second shortcut” at the end.)
1. Folder layout
ros2-docker/
├─ Dockerfile
├─ entrypoint.sh
└─ .dockerignore # optional
2. Dockerfile
# ---------- base ----------
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-c"]
# ---------- locale (recommended by ROS docs) ----------
RUN apt-get update && \
apt-get install -y locales && \
locale-gen en_US en_US.UTF-8 && \
update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
ENV LANG=en_US.UTF-8 \
LC_ALL=en_US.UTF-8
# ---------- utilities ----------
RUN apt-get update && apt-get install -y \
curl gnupg lsb-release git build-essential
# ---------- ROS 2 repo & key ----------
RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key | \
gpg --dearmor -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 $(lsb_release -cs) main" \
| tee /etc/apt/sources.list.d/ros2.list
# ---------- install ROS 2 ----------
ARG ROS_DISTRO=humble # <— change to iron/foxy/etc if you wish
RUN apt-get update && apt-get install -y \
ros-${ROS_DISTRO}-desktop \
python3-colcon-common-extensions \
&& rm -rf /var/lib/apt/lists/*
# ---------- “source” it for every shell ----------
RUN echo "source /opt/ros/${ROS_DISTRO}/setup.bash" >> /etc/bash.bashrc
# ---------- env-vars you may want ----------
ENV ROS_DOMAIN_ID=0 # or whatever fits your network
ENV ROS_LOCALHOST_ONLY=1 # 0 = allow LAN discovery
# ---------- entrypoint ----------
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
# ---------- default command ----------
CMD ["bash"]
3. entrypoint.sh
#!/usr/bin/env bash
set -e
# Source ROS 2 setup
source "/opt/ros/${ROS_DISTRO}/setup.bash"
# Source your own workspace if it exists
if [ -f "/workspace/install/setup.bash" ]; then
source "/workspace/install/setup.bash"
fi
exec "$@"
4. Build the image
cd ros2-docker
docker build -t my_ros2:humble .
Add --build-arg ROS_DISTRO=iron (or whatever) to swap distros.
5. Run the container
Common development pattern (bind-mount your workspace):
# Create a workspace on host if you don't have one yet
mkdir -p ~/ros2_ws/src
docker run -it --rm \
--name ros2-dev \
--network host \ # for seamless DDS discovery
-v ~/ros2_ws:/workspace \ # mount your catkin/colcon workspace
my_ros2:humble
Extras:
• Need GPU? --gpus all (NVIDIA) or --device=/dev/dri (Intel).
• Real hardware (serial, CAN, …): add --privileged or specific --device flags.
• Persistent container (no --rm) + docker exec -it ros2-dev bash for later sessions.
6. docker-compose (optional)
docker-compose.yml
version: "3.8"
services:
ros2:
image: my_ros2:humble
container_name: ros2-dev
network_mode: host
volumes:
- ~/ros2_ws:/workspace
environment:
- ROS_DOMAIN_ID=0
- ROS_LOCALHOST_ONLY=1
command: bash # run interactive shell
Then docker compose up.
7. The 30-second shortcut
No customisation needed? Use the official image straight from OSRF:
docker run -it --rm \
--network host \
osrf/ros:humble-desktop bash
You’ll land in a fully-featured ROS 2 environment (but without your own code/workspace).
8. Tips & gotchas
• VS Code remote-containers: point it at this Dockerfile and it just works.
• DDS discovery across machines: use --network host (Linux-only) or set up ros2 multicast send / ROS_DOMAIN_ID.
• Always keep apt-get update + clean in one layer to minimize image size.
• If you build from source inside the container, remember to colcon build --parallel-workers $(nproc) to squeeze all CPU juice.
Happy hacking 🤖🚀