Cloud Computing 7 min read

Docker Image Optimization: Reducing Size from 1.16 GB to 22.4 MB

This article explains how to shrink a Docker image for a React application from 1.16 GB to 22.4 MB by using a lightweight Alpine base, multi‑stage builds, and finally serving the compiled assets with Nginx, providing step‑by‑step Dockerfile examples and size comparisons.

Top Architect
Top Architect
Top Architect
Docker Image Optimization: Reducing Size from 1.16 GB to 22.4 MB

Docker is a platform that lets developers and system administrators build, run, and share applications in isolated containers, each based on a Docker image that contains everything needed to run the app. Containers are popular because they are flexible, lightweight, portable, loosely coupled, and provide process isolation.

The article focuses on optimizing Docker images to make them as small as possible, using a React application as a concrete example.

Step 1 – Baseline Dockerfile : A simple Dockerfile based on the node:10 image produces a 1.16 GB image.

FROM node:10
WORKDIR /app
COPY app /app
RUN npm install -g webserver.local
RUN npm install && npm run build
EXPOSE 3000
CMD webserver.local -d ./build

Step 2 – Use a lightweight base image : Switching the base to node:10-alpine reduces the image size to about 330 MB.

FROM node:10-alpine
WORKDIR /app
COPY app /app
RUN npm install -g webserver.local
RUN npm install && npm run build
EXPOSE 3000
CMD webserver.local -d ./build

Step 3 – Multi‑stage build : By separating the build phase from the runtime phase, only the compiled assets are kept in the final image, shrinking it further to roughly 91.5 MB.

FROM node:10-alpine AS build
WORKDIR /app
COPY app /app
RUN npm install && npm run build

FROM node:10-alpine
WORKDIR /app
RUN npm install -g webserver.local
COPY --from=build /app/build ./build
EXPOSE 3000
CMD webserver.local -d ./build

Step 4 – Serve with Nginx : Replacing the Node runtime with nginx:stable-alpine and copying only the static build files yields a final image of about 22.4 MB.

FROM node:10-alpine AS build
WORKDIR /app
COPY app /app
RUN npm install && npm run build

FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

The article includes size comparison charts for different base images (Ubuntu, Alpine, Node, Node‑Alpine) and provides links to Docker documentation for further reading.

By applying these three optimizations—choosing a minimal base image, leveraging multi‑stage builds, and using a dedicated web server—the Docker image size drops from 1.16 GB to 22.4 MB while preserving full functionality.

dockerimage optimizationcontainerizationNginxAlpinemultistage-build
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.