Complete rebuild for multi-container - can it be managed?

We have a multi-container system and there are two containers, one of which has a multi-stage build process. Even when there are no changes to that container, it is rebuilt. It takes a long time (a complex build) and I wonder if there is a way to only get the changed container(s) to re-build?

Tips most welcome and thank you.

Hi @branchespark, are you able to share your Dockerfile? It should not be the case that the container is rebuilt every time, it should pull from the cache if there are no changes, so maybe something is going awry.

Hi there. The Dockerfile first builds python libraries and also a custom cp210x library (that is then copied to a shared folder on the hostOS and manually installed once).

Here is the Dockerfile:

#############
# python modules
#############

# base OS for building
FROM raspbian/stretch as build

# enable device access (may also be in docker-compose.yml)
ENV UDEV=on

# install dependencies
RUN apt -y update && \
    apt -y install curl wget git python3 python3-dev python3-pip \
        build-essential linux-headers-rpi \
        libssl-dev libelf-dev libffi-dev awscli zlib1g-dev kmod && \
    apt -y autoremove && \
    apt -y clean

# copy the source files
WORKDIR /usr/src/app
COPY . ./

# bless scripts
RUN chmod a+x ./scripts/*

# build the cp210x driver + balena headers
ENV HEADER_VERSION '2.29.2+rev2.dev'
RUN ./cp210x/cp210x-build.sh raspberrypi3 ${HEADER_VERSION} cp210x
RUN cp ./cp210x_raspberrypi3_${HEADER_VERSION}/cp210x.ko ./cp210x/

# install the python requirements
WORKDIR /usr/src/app/xxx
RUN pip3 install -r requirements.txt

# create new image (run)
FROM raspbian/stretch

# enable device access (may also be in docker-compose.yml)
ENV UDEV=on

# header version for file copy
ENV HEADER_VERSION '2.29.2+rev2.dev'

# packages
RUN apt -y update && \
    apt -y install python3 curl wget git && \
    apt -y autoremove && \
    apt -y clean

# copy python libs
RUN mkdir -p /usr/local/lib/python3.5/dist-packages
WORKDIR /usr/local/lib/python3.5/dist-packages
COPY --from=build /usr/local/lib/python3.5/dist-packages ./

# copy other files
WORKDIR /usr/src/app
COPY . ./

# bless scripts
RUN chmod a+x ./scripts/*

# copy over the build module
COPY --from=build /usr/src/app/cp210x/cp210x.ko ./scripts/

# clean cp210x folder
RUN rm -rf /usr/src/app/cp210x

# run the app (when container live)
CMD ["bash", "/usr/src/app/scripts/start.sh"]

It does this for both local and balenaCloud builds, if that helps?