5

I had tried building a PoC Happstack executable running in Google App Engine using this Dockerfile:

FROM ubuntu:14.04

ENV APP_ROOT=/usr/share/app

RUN apt-get update && apt-get install curl -y && curl -sSL https://get.haskellstack.org/ | sh

COPY . ${APP_ROOT}/
WORKDIR ${APP_ROOT}/
RUN stack setup
RUN stack build

EXPOSE 8000

ENTRYPOINT ["stack","exec","app-exe"]

This works and I was able to deploy, but the resulting image seems huge.

I think the image is about 450MB following the stack installation, about 1.8GB following stack setup, and about 3GB following stack build.

I think hundreds of MB seems reasonable, even up to a GB. Is there a different approach I should be taking, perhaps extracting the resulting executable to another image somehow to eliminate everything unnecessary at runtime?

ryachza
  • 4,460
  • 18
  • 28

1 Answers1

7

This is perfect for docker multi stage builds:

https://docs.docker.com/develop/develop-images/multistage-build/

You can apply as follows:

FROM ubuntu:14.04 as mybuild

ENV APP_ROOT=/usr/share/app

RUN apt-get update && apt-get install curl -y && curl -sSL https://get.haskellstack.org/ | sh

COPY . ${APP_ROOT}/
WORKDIR ${APP_ROOT}/
RUN stack setup
RUN stack build

FROM ubuntu:14.04

COPY --from=mybuild /path/to/app-exe /dest/app-exe #edit this line accordingly

EXPOSE 8000

ENTRYPOINT ["stack","exec","app-exe"]

Everything before the second FROM is not included in the final image, except what you copy with COPY --from.

Robert
  • 33,429
  • 8
  • 90
  • 94
  • 5
    If you want to really go nuts, here's a blog post about making a Docker image with a Haskell executable in < 5 MB: https://www.fpcomplete.com/blog/2015/05/haskell-web-server-in-5mb – Emanuel Borsboom Mar 11 '18 at 16:33
  • Thanks this works sorry I was trying to test it but had also been trying to switch to 16.04 which caused some trouble. I had seen the `FROM+COPY` somewhere but missed the `AS` so didn't follow where `--from` came from. – ryachza Mar 11 '18 at 22:49