Skip to content

Commit

Permalink
solved caching for building a custom package
Browse files Browse the repository at this point in the history
  • Loading branch information
klaxalk committed Oct 2, 2024
1 parent cf7febb commit ea2e845
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
40 changes: 29 additions & 11 deletions docker/custom_package/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
FROM ctumrs/mrs_uav_system:robofly
ARG WORKSPACE_PATH
ARG BASE_IMAGE=ctumrs/mrs_uav_system:latest

ARG WORKSPACE_PATH="/etc/robofly/catkin_workspace"
#############################################################################

# create catkin workspace
RUN mkdir -p ${WORKSPACE_PATH} && cd ${WORKSPACE_PATH} && rm -rf ./* && mkdir src
RUN cd ${WORKSPACE_PATH} && catkin init
# FIRST STAGE: BUILD THE WORKSPACE
FROM $BASE_IMAGE AS stage_build

ARG WORKSPACE_PATH

# set workspace build profile
RUN cd ${WORKSPACE_PATH} && catkin config --profile reldeb --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && catkin profile set reldeb && catkin config --extend /opt/ros/noetic
COPY ./cache/${WORKSPACE_PATH}/ /${WORKSPACE_PATH}/

# create catkin workspace
RUN [ ! -e /${WORKSPACE_PATH}/.catkin_tools ] && cd /${WORKSPACE_PATH} && catkin init && catkin config --profile reldeb --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && catkin profile set reldeb && catkin config --extend /opt/ros/noetic || echo "[Docker]: catkin workspace already exists"

# copy the sources from the local subfolder
COPY src/ ${WORKSPACE_PATH}/src/
COPY src/ /${WORKSPACE_PATH}/src/

RUN cd /${WORKSPACE_PATH} && catkin build -s

#############################################################################

# SECOND STAGE: COPY THE WORKSPACE TO A BLANK APLINE IMAGE
FROM alpine AS stage_cache_workspace

ARG WORKSPACE_PATH

COPY --from=stage_build /${WORKSPACE_PATH} /${WORKSPACE_PATH}

#############################################################################

RUN ls ${WORKSPACE_PATH}/src/
# THIRD STAGE: copy the workspace to a final blank ROS-equipped base image
FROM $BASE_IMAGE AS stage_finalization

RUN cd ${WORKSPACE_PATH} && catkin build -s
ARG WORKSPACE_PATH

RUN mkdir -p /etc/robofly/ws && cp -r ${WORKSPACE_PATH}/* /etc/robofly/ws/
COPY --from=stage_cache_workspace /${WORKSPACE_PATH} /${WORKSPACE_PATH}

CMD ["bash"]
18 changes: 15 additions & 3 deletions docker/custom_package/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#!/bin/bash

LOCAL_TAG=robofly:custom_package
REGISTRY=fly4future

docker build . --file Dockerfile --tag $REGISTRY/$LOCAL_TAG --no-cache --progress=plain
ARCH=arm64
# ARCH=amd64

# docker push $REGISTRY/$LOCAL_TAG
WORKSPACE_PATH=etc/catkin_workspace
CACHE_PATH=cache

# initialize the cache
mkdir -p ./${CACHE_PATH}/${WORKSPACE_PATH}

# this first build compiles the contents of "src" and storest the intermediate
# catkin workspace in ./cache
docker build . --target stage_cache_workspace --output ./${CACHE_PATH} --build-arg WORKSPACE_PATH=${WORKSPACE_PATH} --file Dockerfile --platform=linux/arm64

# this second build takes the resulting workspace and storest in in a final image
# that can be deployed to a drone
docker build . --target stage_finalization --file Dockerfile --build-arg WORKSPACE_PATH=${WORKSPACE_PATH} --tag $LOCAL_TAG --platform=linux/arm64
11 changes: 0 additions & 11 deletions docker/custom_package/build_arm64.sh

This file was deleted.

0 comments on commit ea2e845

Please sign in to comment.