0
$\begingroup$

I'm building ros2 humble from source on a Docker image. The target is an arm32v7 computer (Odroid XU4) I'm using rosinstall_generator to get ros_core packages. Using docker buildx I managed to build the image on my laptop PC, so far, so good. What should I do to reduce the size of the image?

# ARG is overridable by --build-arg
ARG UBUNTU_CODENAME=jammy
ARG ROS_DISTRO=humble

FROM arm32v7/ubuntu:${UBUNTU_CODENAME}

# NOTE: An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM.
# To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage
ARG UBUNTU_CODENAME
ARG ROS_DISTRO

# Set locale
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 \
    && rm -rf /var/lib/apt/lists/*

ENV LANG en_US.utf8
# ENV DEBIAN_FRONTEND=noninteractive

# Add the ROS 2 apt repository
RUN apt-get update && apt-get install -y software-properties-common \
    && add-apt-repository universe \
    && rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y curl \
    && curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg \
    && rm -rf /var/lib/apt/lists/*

RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu ${UBUNTU_CODENAME} main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null

# Por defecto se utiliza la timezone de Buenos Aires
ENV TZ=America/Argentina/Buenos_Aires
RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone

# Install development tools and ROS tools
RUN apt-get update && apt-get install -y \
    python3-flake8-docstrings \
    python3-pip \
    python3-pytest-cov \
    ros-dev-tools \
    && rm -rf /var/lib/apt/lists/*

# install some pip packages needed for testing
RUN apt-get update && apt-get install -y \
    python3-flake8-blind-except \
    python3-flake8-builtins \
    python3-flake8-class-newline \
    python3-flake8-comprehensions \
    python3-flake8-deprecated \
    python3-flake8-import-order \
    python3-flake8-quotes \
    python3-pytest-repeat \
    python3-pytest-rerunfailures \
    && rm -rf /var/lib/apt/lists/*


# Install latest cmake
RUN apt-get update && apt-get install -y gpg wget \
    && wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ ${UBUNTU_CODENAME} main" | tee /etc/apt/sources.list.d/kitware.list >/dev/null \
    && apt-get update \
    && apt-get install -y cmake \
    && rm -rf /var/lib/apt/lists/*

COPY core.repos /ros2.repos

# Get ROS 2 code
RUN update-ca-certificates --fresh \
    && mkdir -p /root/ros2_ws/src \
    # && wget https://raw.githubusercontent.com/ros2/ros2/${ROS_DISTRO}/ros2.repos \
    && vcs import --input ros2.repos /root/ros2_ws/src

# Install dependencies using rosdep
RUN apt-get update && apt-get upgrade -y \
    && update-ca-certificates --fresh \
    && rosdep init \
    && rosdep update \
    && rosdep install --from-paths /root/ros2_ws/src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers" \
    && rm -rf /var/lib/apt/lists/*

RUN cd /root/ros2_ws && colcon build && rm -rf build log src

Doing a docker history I get:

IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
12b7ac1e8625   4 minutes ago   RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   138MB     buildkit.dockerfile.v0
<missing>      2 hours ago     RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   337MB     buildkit.dockerfile.v0
<missing>      2 hours ago     RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   2.28GB    buildkit.dockerfile.v0
<missing>      2 hours ago     COPY core.repos /ros2.repos # buildkit          26.8kB    buildkit.dockerfile.v0
<missing>      2 hours ago     RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   39.2MB    buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   2.76MB    buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   412MB     buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   31B       buildkit.dockerfile.v0
<missing>      20 hours ago    ENV TZ=America/Argentina/Buenos_Aires           0B        buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   118B      buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   2.05MB    buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   110MB     buildkit.dockerfile.v0
<missing>      20 hours ago    ENV LANG=en_US.utf8                             0B        buildkit.dockerfile.v0
<missing>      20 hours ago    RUN |2 UBUNTU_CODENAME=jammy ROS_DISTRO=humb…   20.8MB    buildkit.dockerfile.v0
<missing>      20 hours ago    ARG ROS_DISTRO                                  0B        buildkit.dockerfile.v0
<missing>      20 hours ago    ARG UBUNTU_CODENAME                             0B        buildkit.dockerfile.v0
<missing>      2 weeks ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      2 weeks ago     /bin/sh -c #(nop) ADD file:6767efafdb51cef2a…   56.4MB    
<missing>      2 weeks ago     /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        
<missing>      2 weeks ago     /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        
<missing>      2 weeks ago     /bin/sh -c #(nop)  ARG LAUNCHPAD_BUILD_ARCH     0B        
<missing>      2 weeks ago     /bin/sh -c #(nop)  ARG RELEASE                  0B   

If I understand right # Get ROS 2 code takes 2.28GB?? and rosdep takes 337 MB?

Is there a way to reduce that?

$\endgroup$

1 Answer 1

0
$\begingroup$

The short answer here is to remove the source workspace as well as any build only dependencies and intermediate artifacts.

The bigger picture is that you should be looking at using the generated install directory from your build and installing that instead of just using the development workspace which is not optimized for execution/deployment and still had all the artifacts necessary to build again.

There are many techniques for this. You can see examples in cross compilation toolchains, or the debian packages.

$\endgroup$
1
  • $\begingroup$ Thank for your answer. As you mentioned I was trying development image as a base for production image. I managed to reduce final image size a lot. $\endgroup$ Commented Mar 5 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.