I don't know if you guys are interested in DevOps, but while setting up Docker for the project and searching for how to install stm8flash (an open-source software to flash STM8 chips), I found a repository using a nice trick I didn’t know about - multi-stage builds. I've used it here.
Some software pieces are not available as Linux packages and need to be downloaded and installed manually in the Dockerfile. However, this approach leaves the downloaded source code and potentially the tools required for compilation in the final image. This isn’t a huge problem, but it’s not ideal either.
With multi-stage builds, we start as usual by selecting a base for the image. For me, it’s (note that I’ve labeled this as "build"):
FROM ubuntu:20.04 AS build
Then, we download the code we want to compile, compile it, and start "fresh" again with a new base (this time labeled as "runtime"):
FROM ubuntu:20.04 AS runtime
Our Docker image won’t include any modifications we made in the previous stage, but we can explicitly copy the binaries from the earlier labeled stage:
COPY --from=build /stm8flash/stm8flash /usr/bin/
Our final Docker image will only contain the files we’ve copied - no intermediate files or unnecessary build tools!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.