dockernginx

Why isn't my Docker image of nginx:alpine (with nothing added) about 40 mb instead of 5mb?


I'm trying to understand why my Docker image of nginx:alpine is about 42mb and not about 5mb?

I've also checked out nginx:alpine-slim which is smaller, but at the 12mb size.

enter image description here

Is this because

Playing around with the nginx:alpine-slim version .. I tried du -sh to get the disk usage from the /usr folder .. guessing that this is where all (most?) of the nginx executables are?

enter image description here

So 5mb (base alpine image) + 7mb (this folder + sub-folders) would equal about the 12mb image file size .. which would make sense.

Let's check out the nginx:alpine equivalent:

enter image description here

Whoa! 37mb, so 5mb + 37mb is about the 42mb.

So if I'm reading this info correctly: why is there such a massive difference in the /usr folders?


Solution

  • The Dockerfiles for building these images are public, you can click the tag in Docker Hub to be taken directly to the Dockerfile. For nginx:alpine, that Dockerfile includes

    FROM nginx:1.25.2-alpine-slim
    

    so it is extending the slim image. And then it goes on to install various packages:

    ENV NJS_VERSION   0.8.0
    
    RUN set -x \
        && apkArch="$(cat /etc/apk/arch)" \
        && nginxPackages=" \
            nginx=${NGINX_VERSION}-r${PKG_RELEASE} \
            nginx-module-xslt=${NGINX_VERSION}-r${PKG_RELEASE} \
            nginx-module-geoip=${NGINX_VERSION}-r${PKG_RELEASE} \
            nginx-module-image-filter=${NGINX_VERSION}-r${PKG_RELEASE} \
            nginx-module-njs=${NGINX_VERSION}.${NJS_VERSION}-r${PKG_RELEASE} \
        " \
    # install prerequisites for public key and pkg-oss checks
        && apk add --no-cache --virtual .checksum-deps \
            openssl \
        && case "$apkArch" in \
    # ... rest of the file can be see in the GitHub link above ...
    

    If you don't need those specific nginx-module packages or openssl in your image, then you only need nginx:alpine-slim.

    For images where you can't look at the Dockerfile directly, many will include details in the image config that can be viewed with docker image history. This also lists the size of each of the layers so you can see which command added how much space.