milesmcc / shynet

Modern, privacy-friendly, and detailed web analytics that works without cookies or JS.
Apache License 2.0
2.89k stars 183 forks source link

Optimized Dockerfile to reduce uncompressed image size #276

Closed jarylc closed 1 year ago

jarylc commented 1 year ago

Combining apk del commands in the same layer as their add commands shaves off ~270MB when uncompressed.

❯ docker images
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
<none>            <none>    16b82802079b   9 minutes ago    1.64GB <- new
milesmcc/shynet   latest    945cbbe7305b   8 days ago       1.91GB <- current
milesmcc commented 1 year ago

Thanks!

jarylc commented 1 year ago

I'm quite curious what caused the compressed image size to go from 200-300MB to jump to 750MB from version v0.10.0 to v0.11.0 though.

When uncompressed right now, the image is taking up 1/10 of the space of my 20GB VPS. Which ultimately inspired me to do this PR, but I wonder if we can find a way to reduce the image size even more after this is merged.

EDIT 1: Looking at the commit diffs, it seems it's after the addition of rust might be attributed to the rise in image size.

# libffi-dev and rust are used for the cryptography package,
# which we indirectly rely on. Necessary for aarch64 support.

If it's not needed in amd64, maybe we could do an architecture check during build time (using arch or uname -m) and determine if libffi-dev, rust, and cargo are required.

EDIT 2: I tried building the initial apk installation layer like this instead:

RUN apk update && \
    apk add --no-cache gettext bash npm postgresql-libs && \
    test "$(arch)" != "x86_64" && apk add libffi-dev rust cargo || echo "amd64 build, skipping Rust installation"
    # libffi-dev and rust are used for the cryptography package,
    # which we indirectly rely on. Necessary for aarch64 support.

This yields even better sizes for amd64 systems, but unfortunately arm64 still has to take a hit unless we can remove rust as a dependency entirely.

 ❯ docker images                                                                                                                                                                                                                                                                                    [08:32:47]
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    2cda0b101cd3   10 seconds ago   744MB <- rust removed
<none>       <none>    41af2fc866fc   3 minutes ago    1.64GB <- with rust

@milesmcc @CasperVerswijvelt do let me know if you guys are okay with this approach, I can append this change to this PR as well.

CasperVerswijvelt commented 1 year ago

@jarylc while I'm not really familiar with all the dependencies of Shynet, if the libffi-dev and rust are used for the cryptography package, which we indirectly rely on. Necessary for aarch64 support comment is accurate, I am all for this change.

I'm also curious why such huge dependencies are needed at all, just for the cryptography package (and why they are only needed when building for aarch64). Could you maybe shed some more light on that, @milesmcc?

milesmcc commented 1 year ago

I don't entirely remember; I think the issue was that for the aarch64 build, a lot of binaries do not come pre-compiled, so we need to install the rust/libffi-dev toolchains to compile those dependencies ourselves. Perhaps the story has gotten better since then.

If aarch64 builds (and runs) correctly without rust or libffi-dev, then that suggests that precompiled binaries are now available for aarch64, in which we can drop those deps.

jarylc commented 1 year ago

I'll try to find some time to test removing the dependencies with my RPI4 sometime soon.

jarylc commented 1 year ago

I'm currently testing 1628820 live on my websites now (on amd64), will update EoD if any issues

EDIT: Alright, looks like all is good on amd64 without these 2 dependencies, shaving half of the image size! Hope we can get this change in first.