tweag / asterius

DEPRECATED in favor of ghc wasm backend, see https://www.tweag.io/blog/2022-11-22-wasm-backend-merged-in-ghc
1.97k stars 58 forks source link

Reduce size of docker image #70

Closed majkrzak closed 5 years ago

majkrzak commented 5 years ago

Currently docker image have crazy size of 1 GB. Will be nice to reduce it to some reasonable one.

One of possible approach is to use lightweight base, but I would rather say that it will be the best to create second stage where only needed dependencies will be copied.

TerrorJack commented 5 years ago

I agree with the objective of cutting down image sizes.

The base image is debian sid official image, which is not large iirc. The biggest problem is: trimming unneeded stuff in resulting image is a non-trivial surgery which can easily go wrong. We set up a bunch of deps, run stack build then boot, and then there's gigantic ~/.stack and .stack-work directories you can't simply wipe, because at run time ahc-link and its friends rely on the host ghc and some data files created during booting.

I think the best workaround at this moment is doing some recursive analysis on an image, discover potentially large unneeded stack build directories and add shell scripts to prune them in the dockerfile. Will try this when I'm less occupied; if you're interested and would like to spend some time, I'd like to help and answer questions here :)

majkrzak commented 5 years ago

Basically that was my idea and so here is my first attempt:

FROM terrorjack/asterius AS builder

RUN mkdir -p /out/bin/; whereis -b ahc-link | grep -Po '(?<= ).*' | xargs -I '{}' cp -v '{}' /out/bin/
RUN mkdir -p /out/lib64/; ldd /out/bin/ahc-link | grep -Po '/.*(?= )' | xargs -I '{}' cp -v '{}' /out/lib64/

FROM scratch

COPY --from=builder /out/ /
ENV LD_LIBRARY_PATH /lib64

ENTRYPOINT ["/bin/ahc-link"]
CMD []

It let you display help from ahc-link, I'm not sure if it is possible to compile something without additional "soft" dependencies :man_shrugging:

majkrzak commented 5 years ago
[INFO] Loading boot library store from "/root/asterius/.stack-work/install/x86_64-linux/ghc-8.7/8.7.20190217/share/x86_64-linux-ghc-8.7.20190217/asterius-0.0.1/.boot/asterius_lib/asterius_store"
ahc-link: /root/asterius/.stack-work/install/x86_64-linux/ghc-8.7/8.7.20190217/share/x86_64-linux-ghc-8.7.20190217/asterius-0.0.1/.boot/asterius_lib/asterius_store: openBinaryFile: does not exist (No such file or directory)

oeps :sweat_smile: I see my mistake

TerrorJack commented 5 years ago

Thanks. Attempting to do a multi-stage build here isn't realistic; ahc-link relies on ghc, which in turn relies on a hell lot of things in the image. So pruning instead of picking is more pragmatic :)

TerrorJack commented 5 years ago

Closing for now. Figuring out all runtime dependencies of ahc-link is a non-trivial task, and imho we'll need to endure the large images for the moment, and wait for nix support to be merged upstream.