Shrinking Haskell Docker images using multi-stage builds

Liam Griffin · 21 Dec 2017

I have recently discovered Docker's new multi-stage build feature. This has been a great help in answering my question of how to reduce the size of my haskell images for deploying, as the main haskell image on Docker Hub is over 1GB before you start adding things. This also means you don't have to maintain two Dockerfiles.

With multi stage builds we can use the haskell docker image to build our distributable executable, then copy this to a smaller image (in this case phusion/baseimage).

Here's an example Dockerfile for a project called haskell-example:

FROM haskell as build-env
WORKDIR /opt/server
RUN cabal update
COPY haskell-example.cabal .
RUN cabal install --only-dependencies -j4
ADD . .
RUN cabal install

FROM phusion/baseimage
WORKDIR /opt/server
COPY --from=build-env /opt/server/dist .

CMD ./build/haskell-example/haskell-example

Before, using just the haskell image: 1.87GB.

After, using multi-stage with baseimage: 249MB.

Image caching

There's just one problem with this method - the intermediate images are all marked as dangling. This means that if you do a docker system prune and rebuild, you will have install all your dependencies again, which takes a lot of time in the haskell world.

The workaround for this is to build an image using the build-env stage. This is done by specifying the target when building.

docker build -t haskell-example-dev --target build-env .

This keeps the image in the cache so it won't be prune'd.

Next steps

So we've shrunk our deployable image using phusion/baseimage, can we go further? The next step would be to use something like alpine as the production image, a mere 4.14MB. That's what I'll talk about next time :)


This post was cross-posted to my personal blog.

Liam Griffin Image

Liam Griffin

Liam is a developer with experience building cloud-based .NET applications on Azure, but is also interested in functional languages such as Haskell. After moving to London 2 years ago he became involved in the London Software Craftsmanship Community and particularly loves the hands-on sessions.

Liam is keen to adopt the benefits of functional programming styles in his use of OOP focused languages, such as C#, and share this knowledge with others.

All author posts
Codurance Logo

Software is our passion.

We are software craftspeople. We build well-crafted software for our clients, we help developers to get better at their craft through training, coaching and mentoring, and we help companies get better at delivering software.

Latest Blogs




Contact Us

15 Northburgh Street
London EC1V 0JR

Phone: +44 207 4902967

2 Mount Street
Manchester M2 5WQ

Phone: +44 207 4902967

Carrer Aragó, 208
08011, Barcelona

Phone: +34 937 82 28 82

Email: hello@codurance.com