Monday, March 14, 2022

[SOLVED] Optimize docker image build size with curl

Issue

I need to install on docker the latest version of curl

when using the following the docker size is ~140MB

FROM debian:10.7

RUN apt-get update && \
    apt-get install --no-install-recommends -y curl wget ca-certificates

This use curl 7.64

when using the following

FROM debian:10.7

RUN apt-get update && \
    apt-get install --yes --no-install-recommends wget build-essential ca-certificates libcurl4 && \
    wget https://curl.se/download/curl-7.73.0.tar.gz && \
    tar -xvf curl-7.73.0.tar.gz && cd curl-7.74.0 && \
    ./configure && make && make install && \
    apt-get purge -y --auto-remove build-essential && \

The docker image size is 240MB, I've tried to remove the build essintials which reduce the size from 440 to 240 , is there a way to remove this additional ~100MB ?


Solution

In fact, you are close to the solution. The one you missed is to delete the curl source package.

So next should make the image reduce:

FROM debian:10.7

RUN apt-get update && \
    apt-get install --yes --no-install-recommends wget build-essential ca-certificates libcurl4 && \
    wget https://curl.se/download/curl-7.73.0.tar.gz && \
    tar -xvf curl-7.73.0.tar.gz && cd curl-7.73.0 && \
    ./configure && make && make install && \
    apt-get purge -y --auto-remove build-essential && \
    cd .. && rm -fr curl-7.73.0.tar.gz curl-7.73.0
    

Without Curl:

$ docker images abc:1
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
abc                 1                   d742bfdf5fa6        25 seconds ago      148MB

With curl & source package delete:

$ docker images abc:2
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
abc                 2                   afe3d404852a        27 minutes ago      151MB

Additional, if you delete apt cache with rm -rf /var/lib/apt/lists/* in Dockerfile, if will be smaller:

$ docker images abc:3
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
abc                 3                   5530b0e9b44f        2 minutes ago       134MB

Another solution maybe use multistage-build, you could use ./configure --prefix=xxx to set a default install location, then stage1 just used to build curl, while stage2 copy the xxx folder from stage1 to final image.



Answered By - atline
Answer Checked By - Willingham (WPSolving Volunteer)