Lightweight OCaml Docker Images with Multi-Stage Builds



Hi! I’ve written an article of me exploring Docker multi-stage builds feature with OCaml. Feedbacks are welcome!

Source-to-image OCaml builder

The article has just been updated to fix the missing depexts installation on the deployment image, which may result in missing required OS libs that are not statically linked. It bumped the image size a bit, but still lightweight. Kudos to @Leonidas for the feedback!


Great article, thanks for writing this up. An other option is to use -cclib -static to produce a self-contained binary, and copy this into a scratch image:

This needs some tweaks to the build system (to pass that option only when building the image, as for instance -static will not work on OSX) but it is worth it for a very minimal deployment.


Oh, thanks for the info! This is a weekend hack for me so I have only taken minimal effort so far. Nice to figure out other methods along the way.

There are many questions in my head right now. How self-contained is “self-contained”, i.e. would installing depexts not be necessary if I used this? Does it have disadvantages compared to what I’m doing right now (other than I assume bigger binary size)? Are there established best practices on how we should package our application binary?

Not pressing you to answer all of them, though, just thinking out loud :slight_smile: would be glad if the community could point me towards friendly resources… the native compilation chapter of RWO was kinda overwhelming for me.

I’ve never worked with a natively-compiled language before other than a tiny bit of C in Uni, so all this is new to me. Really exciting!