Skip to content

Instantly share code, notes, and snippets.

@lfborjas
Forked from TimWSpence/Dockerfile
Created December 14, 2020 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfborjas/28d1eb02e70b1b13407eabfa8bb25974 to your computer and use it in GitHub Desktop.
Save lfborjas/28d1eb02e70b1b13407eabfa8bb25974 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