Skip to content

Instantly share code, notes, and snippets.

@TimWSpence
Last active August 16, 2022 08:03
Show Gist options
  • Save TimWSpence/9b89b0915bf5224128e4b96abfd4ce02 to your computer and use it in GitHub Desktop.
Save TimWSpence/9b89b0915bf5224128e4b96abfd4ce02 to your computer and use it in GitHub Desktop.
Optimized multistage Dockerfile for Haskell Stack builds
# Loosely based on https://www.fpcomplete.com/blog/2017/12/building-haskell-apps-with-docker
FROM fpco/stack-build:lts-13.27 as dependencies
RUN mkdir /opt/build
WORKDIR /opt/build
# GHC dynamically links its compilation targets to lib gmp
RUN apt-get update \
&& apt-get download libgmp10
RUN mv libgmp*.deb libgmp.deb
# Docker build should not use cached layer if any of these is modified
COPY stack.yaml package.yaml stack.yaml.lock /opt/build/
RUN stack build --system-ghc --dependencies-only
# -------------------------------------------------------------------------------------------
FROM fpco/stack-build:lts-13.27 as build
# Copy compiled dependencies from previous stage
COPY --from=dependencies /root/.stack /root/.stack
COPY . /opt/build/
WORKDIR /opt/build
RUN stack build --system-ghc
RUN mv "$(stack path --local-install-root --system-ghc)/bin" /opt/build/bin
# -------------------------------------------------------------------------------------------
# Base image for stack build so compiled artifact from previous
# stage should run
FROM ubuntu:16.04 as app
RUN mkdir -p /opt/app
WORKDIR /opt/app
# Install lib gmp
COPY --from=dependencies /opt/build/libgmp.deb /tmp
RUN dpkg -i /tmp/libgmp.deb && rm /tmp/libgmp.deb
COPY --from=build /opt/build/bin .
EXPOSE 8080
CMD ["/opt/app/app", "8080"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment