Search code examples
dockerfiledebian-buster

shrinking a Docker Image - from debian to scratch - how to migrate?


i am trying to build a minimalistic docker image for one of my applicatoins

in my "usual" builds i do not rely on 3rd party applications. This time I need to include a precompiled executeable (xpdf) to the build; My go applications are prebuilt in a builder Docker and then copied over (no dependencies).

my current Dockerimage file looks like this: (working!) application launches

FROM debian:buster-slim
COPY ./bin64/pdftopng .
COPY ./config/xpdfrc .
# ... copy FONT files
# copy Test PDF
COPY ./test.pdf .
RUN chmod +x pdftopng
RUN apt-get update && apt-get install -y libfontconfig1
# execute pdftopng
ENTRYPOINT ["/pdftopng", "-cfg", "xpdfrc", "/test.pdf", "/test.pdf"]

as this results in a image file with over 100mb i tried to use scratch as a base (which usually results in images around 10-20mb)

FROM scratch
# ... copy my binaries to the docker
COPY --from=builder /dist/main /
# copy the XPDF -> pdf to png executeable (linux x64)
COPY ./bin64/pdftopng .
# copy font files (for this example i only copy one)
COPY ./fonts/n021003l.pfb /fonts/n021003l.pfb
# copy config file
COPY ./config/xpdfrc .
# copy test file
COPY ./test.pdf .
# execute the XPDF converter and convert the test.pdf to png
ENTRYPOINT ["/pdftopng", "-cfg", "xpdfrc", "/test.pdf", "test"]

outputs:

standard_init_linux.go:219: exec user process caused: no such file or directory

this error is the result of a missing library (something related to libfontconfig1) how can i find out which files are missing (as i do have a working container this should be possible)


Solution

  • Solution

    Step 1 - create libsource

    create a docker image where you can grab all required libraries from

    FROM debian:buster-slim AS libsource
    COPY ./bin64/pdftopng .
    COPY ./config/xpdfrc .
    # ... copy FONT files
    # copy Test PDF
    COPY ./test.pdf .
    RUN chmod +x pdftopng
    RUN apt-get update && apt-get install -y libfontconfig1
    

    Step 2 - find requirements

    then obtain a list of libraries which the application [./pdftopng] needs to run

    docker run --rm -it ldd ./pdftopng
    

    this command lists all required libraries (direct use only):

        linux-vdso.so.1 (0x00007ffce1304000)
        libbz2.so.1 => /lib/x86_64-linux-gnu/libbz2.so.1 (0x00007fa1104ea000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fa1102cc000)
        libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007fa110286000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa110265000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa1100e1000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa10ff5e000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa10ff42000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa10fd81000)
        libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007fa10fcc5000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fa10fc88000)
        libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007fa10fc7f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa110501000)
        libpng16.so.16 => /usr/lib/x86_64-linux-gnu/libpng16.so.16 (0x00007fa10fc44000)
    

    Step 3 - copy requirements

    in the last step copy the libraries from step 2 to the scratch image

    COPY --from=libsource /lib/x86_64-linux-gnu/libbz2.so.1 /lib/x86_64-linux-gnu/libbz2.so.1
    COPY --from=libsource /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1
    COPY --from=libsource /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 /usr/lib/x86_64-linux-gnu/libfontconfig.so.1
    COPY --from=libsource /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0
    COPY --from=libsource /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    COPY --from=libsource /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6
    COPY --from=libsource /lib/x86_64-linux-gnu/libgcc_s.so.1  /lib/x86_64-linux-gnu/libgcc_s.so.1
    COPY --from=libsource /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
    COPY --from=libsource /usr/lib/x86_64-linux-gnu/libfreetype.so.6 /usr/lib/x86_64-linux-gnu/libfreetype.so.6
    COPY --from=libsource /lib/x86_64-linux-gnu/libexpat.so.1 /lib/x86_64-linux-gnu/libexpat.so.1
    COPY --from=libsource /lib/x86_64-linux-gnu/libuuid.so.1 /lib/x86_64-linux-gnu/libuuid.so.1
    COPY --from=libsource /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
    COPY --from=libsource /usr/lib/x86_64-linux-gnu/libpng16.so.16 /usr/lib/x86_64-linux-gnu/libpng16.so.16
    

    Result

    in my case the result is a much smaller image 15.45 MB instead of 101.90 MB

    which is a reduction by 84,84 %