From 77ab65ef4a8da28a0a48979a1f35c7d1e5ead067 Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Wed, 16 Mar 2022 00:07:16 +1100 Subject: [PATCH] update dockerfile template - Use Amazon Linux 2 - cache dependencies by separating it to a different step and utilising docker layer caching - add comments on where we can install native dependencies --- stack-template.hsfiles | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/stack-template.hsfiles b/stack-template.hsfiles index d45ec6f..06221d6 100644 --- a/stack-template.hsfiles +++ b/stack-template.hsfiles @@ -67,12 +67,11 @@ handler person context = return (Left "A person's age must be positive") {-# START_FILE Dockerfile #-} +# Loosely based on https://timwspence.github.io/blog/posts/2019-08-02-optimized-docker-builds-for-haskell-stack.html ARG OUTPUT_DIR=/root/output ARG EXECUTABLE_NAME=bootstrap -FROM lambci/lambda:build-provided as build - -COPY . . +FROM lambci/lambda:build-provided.al2 as build SHELL ["/bin/bash", "--rcfile", "~/.profile", "-c"] @@ -83,17 +82,18 @@ RUN yum update -y ca-certificates # Installing Haskell Stack RUN curl -sSL https://get.haskellstack.org/ | sh -# Build the lambda -COPY . /root/lambda-function/ +WORKDIR /root/lambda-function/ -RUN pwd +# Build the deps +RUN stack clean --full +RUN yum install -y gmp-devel ncurses-devel # Add more build time dependencies here if needed. EG: postgresql-devel -RUN cd /root/lambda-function -WORKDIR /root/lambda-function/ +COPY stack.yaml package.yaml stack.yaml.lock /root/lambda-function/ +RUN stack build --dependencies-only -RUN ls +# Build the lambda +COPY . /root/lambda-function/ -RUN stack clean --full RUN stack build ARG OUTPUT_DIR @@ -103,12 +103,15 @@ RUN mkdir -p ${OUTPUT_DIR} && \ ARG EXECUTABLE_NAME -RUN cp $(stack path --local-install-root)/bin/${EXECUTABLE_NAME} ${OUTPUT_DIR}/${EXECUTABLE_NAME} +RUN mv $(stack path --local-install-root)/bin/${EXECUTABLE_NAME} ${OUTPUT_DIR}/${EXECUTABLE_NAME} ENTRYPOINT sh FROM public.ecr.aws/lambda/provided:al2 as deploy +# Install any runtime dependencies. EG: +# RUN yum install -y postgresql + ARG EXECUTABLE_NAME WORKDIR ${LAMBDA_RUNTIME_DIR} @@ -117,9 +120,7 @@ ARG OUTPUT_DIR COPY --from=build ${OUTPUT_DIR} . -RUN ls RUN mv ${EXECUTABLE_NAME} bootstrap || true -RUN ls CMD [ "handler" ]