To create a custom ROS2 Docker container, we will use the following files, each serving a specific purpose in the Docker-based development environment setup:

  • Dockerfile: Defines the Docker image, including the base image, installed packages, and environment configurations.
  • entrypoint.sh: Sets up the environment inside the container when it starts, ensuring ROS2 is ready for use.
  • docker_build.sh: A script that automates the Docker image build process.
  • docker_run.sh: A script to run the Docker container with the appropriate settings.
  • .bashrc: Customizes the bash environment inside the container, making it user-friendly for development.

Dockerfile v2

Gazebo

  • Use the the official ROS2 Humble base image
docker build -t ocr-docker:v2 .
  • Run the container
docker run -it ocr-docker:v2 bash
  • Run Gazebo
docker run -it --rm \
    -e DISPLAY=$DISPLAY \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    ocr-docker:v2
  • Note: Gazebo loaded blank screen, switch to VNC

VNC

https://github.com/Tiryoh/docker-ros2-desktop-vnc

# Base image
# FROM osrf/ros:humble-desktop
FROM tiryoh/ros2-desktop-vnc:humble
 
# Install packages
RUN apt-get update && apt-get install -y \
    ros-humble-gazebo-ros-pkgs \
    && apt-get clean && rm -rf /var/lib/apt/lists/* \
 
# Set environment variables
# ENV DISPLAY=0
 
# Source the ROS installation in all interactive shells
RUN echo "source /opt/ros/humble/setup.bash >> ~/.bashrc"
 
# Default command
CMD ["bash"]
  • Run the docker container and access with port 6080
docker run -p 6080:80 --security-opt seccomp=unconfined --shm-size=512m ocr-docker:v2
  • Check port
docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                  NAMES
d2a644476d1e   ocr-docker:v2   "/bin/bash -c /entry…"   18 seconds ago   Up 17 seconds   0.0.0.0:6080->80/tcp   xenodochial_bartik
version: "2"
services:
  test-container:
    image: ocr-docker:v2  
    container_name: test-container
    environment:
      - DISPLAY=0
    # volumes:
      # - /tmp/.X11-unix:/tmp/.X11-unix  # X11 socket for GUI
    ports:
      - 6080:80 # Map noVNC to port 6080
 

picture 1

Push image to Docker Hub

  • Recommended Hybrid Approach

    • Push the pre-built image to Docker Hub for fast access.
    • Include the Dockerfile in the GitHub repository for transparency and customization.
    • Provide both options in your documentation:
      • “Pull the image from Docker Hub for immediate use."
      • "Clone the repo and build locally if you want to modify the Dockerfile or debug.”
  • Tag the image

docker tag ocr-docker:v2 chxtiodev/ocr-docker:v2
  • Push the image
docker push chxtiodev/ocr-docker:v2
  • Verify image in Docker Hub

chxtiodev/ocr-docker:v2

  • Using the image
docker pull chxtiodev/ocr-docker:v1
docker run chxtiodev/ocr-docker:v1

OCR Docker v3- Multi-platform support

# Base image
# FROM tiryoh/ros2-desktop-vnc:humble
FROM --platform=$TARGETARCH tiryoh/ros2-desktop-vnc:humble
ARG TARGETARCH
 
RUN echo "Building for architecture: $TARGETARCH"
 
# Install packages
RUN apt-get update && apt-get install -y \
    && apt-get clean && rm -rf /var/lib/apt/lists/* 
 
# Set environment variables
# ENV DISPLAY=0
 
# Source the ROS installation in all interactive shells
RUN echo "source /opt/ros/humble/setup.bash >> ~/.bashrc"
 
# Default command
CMD ["bash"]
  • Create a builder image
docker buildx create --use
  • Test multi-architecture build
docker buildx build --platform linux/amd64,linux/arm64 -t ocr-docker:humble .
  • Build just for arm64
docker buildx build --platform linux/arm64 -t ocr-docker:humble --load .
  • Tag the image and push to Docker Hub
docker tag ocr-docker:humble chxtiodev/ocr-docker:humble
docker push chxtiodev/ocr-docker:humble

Emable Gazebo ROS for arm64

RUN echo "building for ${TARGETARCH}"
RUN if [ "${TARGETARCH}" = "arm64" ]; then \
    add-apt-repository -s -y ppa:openrobotics/gazebo11-non-amd64; \
    fi
  • Temp: Run the build with BuildKit’s caching
    • will get warning: “WARNING: No output specified with docker-container driver. Build result will only remain in the build cache. To push result image into registry use —push or to load image into docker use —load”
docker buildx build --platform linux/arm64 -t chxtiodev/ocr-docker:humble .
# Clone and build gazebo_ros_pkgs from source
RUN git clone https://github.com/ros-simulation/gazebo_ros_pkgs.git /ros_workspace/src/gazebo_ros_pkgs && \
    cd /ros_workspace && \
    source /opt/ros/humble/setup.bash && \
    rosdep install --from-paths src --ignore-src --rosdistro $ROS_DISTRO -y && \
    colcon build --symlink-install && \
    rm -rf /var/lib/apt/lists/*
  • Another way
RUN cd /tmp/src && vcs import < /tmp/.devcontainer/lcas.repos
RUN if [ "${TARGETARCH}" = "arm64" ]; then \
        cd /tmp/src; \
        vcs import < /tmp/.devcontainer/gazebo_ros_pkgs.repos; \
    fi
RUN cd /tmp/src && vcs pull
  • .devcontainer/gazebo_ros_pkgs.repos
repositories:
  gazebo_ros_pkgs:
    type: git
    url: https://github.com/ros-simulation/gazebo_ros_pkgs.git
    version: 3.7.0

picture 0


ocr-docker:Nekton

Creating and testing the container locally

  • Create Dockerfile
# Base image
FROM mwoodward6/nekton:humble
 
# Auto-source the workspace and cd into it
RUN echo "source /ocr/dev_ws/install/setup.bash" >> ~/.bashrc \
    && echo "cd /ocr/dev_ws" >> ~/.bashrc
  • Create docker-compose.yml
version: "2"
services:
  test-container:
    image: ocr-docker:humble
    container_name: ocr-humble-nekton
    environment:
      - DISPLAY=:1.0
      - USER=root
    volumes:
      - ../dev_ws/:/ocr/dev_ws/
    ports:
      - 6080:80 # Map noVNC to port 6080
    tty: true
    restart: unless-stopped
  • Build the image for arm64 and load it into local Docker cache
docker buildx build --platform linux/arm64 -t ocr-docker:humble --load .
  • Run the container in detached mode
docker-compose up -d
  • Open an interactive bash shell inside the container
docker exec -it ocr-humble-nekton bash

Push image to Docker Hub

  • Tag the image
docker tag ocr-docker:humble chxtiodev/ocr-docker:humble
  • Push the image
docker push chxtiodev/ocr-docker:humble
  • Verify image in Docker Hub

chxtiodev/ocr-docker:humble

  • Using the image
docker pull chxtiodev/ocr-docker:humble
docker run chxtiodev/ocr-docker:humble