Finally, Dockerfiles!
The recommendation is to use multistage builds and, apparently, they're now supported on the Docker Hub! But, you might still find a reason to write a goofy single-stage build.
I forget where I first saw this pattern, but the idea is to install all of the packages, including dev/build packages, do the work, then mark all of the unnecessary packages as purgeable.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
foo \
bar \
baz \
&& ... build commands...
&& apt-mark auto '.*' > /dev/null \
&& apt-mark manual "bar" \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -fr /var/cache/apt/* /var/lib/apt/lists/*
TBD!