ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Reduce only build-related ROS packages - reduce docker image size

asked 2018-03-06 13:55:07 -0500

basti.hunter gravatar image

updated 2018-03-29 20:16:25 -0500

ahendrix gravatar image

I am building a custom ROS docker image to deploy it on a ubuntu xenial embedded board. The Dockerfile is shown below. When I build the container, the size is about 1,7 GB (which is not nuch bigger than a ros:kinetic-ros-core image anyway.

BUT I need it to be smaller!!! My suggestion is, that it might be possible to remove/uninstall those ROS packages and other sw which is only necessary for building my packages, but not needed after running "catkin build".

Does anyone know how this is done? When I try "apt remove ..." with e.g. the "ros-kinetic-roslint" package, even runtime relevant stuff gets removed. Help, please!

FROM ubuntu
LABEL version="0.0.4"

ENV CATKIN_WS=/root/catkin_ws

RUN apt-key adv --keyserver hkp:// --recv-keys 421C365BD9FF1F717815A3895523BAEEB01FA116
RUN echo "deb xenial main" > /etc/apt/sources.list.d/ros-latest.list

RUN apt-get update && \
    apt-get -y --quiet --no-install-recommends install --allow-unauthenticated \
      build-essential \
      gcc \
      g++ \
      cmake \
      make \
      libusb-1.0-0-dev \
      python-wstool \
      python-catkin-tools \
      ros-kinetic-cpp-common \
      ros-kinetic-actionlib \
      ros-kinetic-actionlib-msgs \
      ros-kinetic-bond \
      ros-kinetic-bondcpp \
      ros-kinetic-catkin \
      ros-kinetic-class-loader \
      ros-kinetic-cmake-modules \
      ros-kinetic-gencpp \
      ros-kinetic-geneus \
      ros-kinetic-genlisp \
      ros-kinetic-genmsg \
      ros-kinetic-gennodejs \
      ros-kinetic-genpy \
      ros-kinetic-geometry-msgs \
      ros-kinetic-image-transport \
      ros-kinetic-message-filters \
      ros-kinetic-message-generation \
      ros-kinetic-message-runtime \
      ros-kinetic-nav-msgs \
      ros-kinetic-nodelet \
      ros-kinetic-pluginlib \
      ros-kinetic-rosbag \
      ros-kinetic-rosbag-storage \
      ros-kinetic-rosbuild \
      ros-kinetic-rosclean \
      ros-kinetic-rosconsole \
      ros-kinetic-roscpp \
      ros-kinetic-roscpp-serialization \
      ros-kinetic-roscpp-traits \
      ros-kinetic-rosgraph \
      ros-kinetic-rosgraph-msgs \
      ros-kinetic-roslaunch \
      ros-kinetic-roslib \
      ros-kinetic-roslint \
      ros-kinetic-roslz4 \
      ros-kinetic-rosmaster \
      ros-kinetic-rosout \
      ros-kinetic-rospack \
      ros-kinetic-rosparam \
      ros-kinetic-rospy \
      ros-kinetic-rostest \
      ros-kinetic-rostime \
      ros-kinetic-rostopic \
      ros-kinetic-rosunit \
      ros-kinetic-sensor-msgs \
      ros-kinetic-smclib \
      ros-kinetic-std-msgs \
      ros-kinetic-std-srvs \
      ros-kinetic-tf2 \
      ros-kinetic-tf2-msgs \
      ros-kinetic-tf2-py \
      ros-kinetic-tf2-ros \
      ros-kinetic-topic-tools \
      ros-kinetic-xmlrpcpp \
      ros-kinetic-mav-comm \
      ros-kinetic-mav-msgs \
      ros-kinetic-image-common \
      ros-kinetic-visualization-msgs \
      libopencv-dev && \
    rm -rf /var/lib/apt/lists/*

# copy my local src directory to the docker image
ADD src /root/catkin_ws/src


RUN catkin config --install --extend=/opt/ros/kinetic -DCMAKE_BUILD_TYPE=Release && \
    catkin build --summarize

RUN rm -rf /root/catkin_ws/src
RUN rm -rf /root/catkin_ws/build
RUN rm -rf /root/catkin_ws/logs
RUN rm -rf /root/catkin_ws/devel

RUN strip --strip-all $(find /root/catkin_ws/install -name "*.so"); exit 0

RUN echo 'source "$CATKIN_WS/install/setup.bash"' >> ~/.bashrc

CMD ["bash"]
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-03-29 20:09:28 -0500

ruffsl gravatar image

updated 2018-03-29 20:26:42 -0500

You could consider using an onbuild to create a minimalistic runtime environment by cherry picking the built package files from the build-dockerfile image layers. Onbuild's intended use is geared towards exporting a minimal compiled application from the build image to provide a slimmed down runtime image:

Docker Reference | Engine Builder: onbuild
Stackoverflow | Dockerfile ONBUILD instruction

Also, because the image size on disk is the sum of the sizes of the layers its constructed from, removing files from a separate RUN-command/image-layer does not reduce the total size of the resulting image. You would need to then flatten the image to achieve this (like when exporting and then importing the image). However onbuild now offers a more elegant solution, and is how most of the Docker community achieves this runtime image reduction.

Stackoverflow | Why are Docker container images so large?

edit flag offensive delete link more

Question Tools



Asked: 2018-03-06 13:55:07 -0500

Seen: 1,013 times

Last updated: Mar 29 '18