Over a million developers have joined DZone.

Shrinking Haskell Docker Images Using Multi-Stage Builds

DZone's Guide to

Shrinking Haskell Docker Images Using Multi-Stage Builds

Want to use the main Haskell Docker image but aren't excited about the nearly 2GB it brings along? See how you can shrink it considerably with multi-stage builds.

· Cloud Zone ·
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

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 to 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 pruned.

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 article was first published on the Codurance blog.

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

haskell ,cloud ,docker images ,multi-stage builds

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}