How to Shrink Docker Images from 1.16 GB to 22 MB with Multi‑Stage Builds
This article explains how to optimize Docker images for a React application by switching to Alpine base images, applying multi‑stage builds, and using Nginx to serve static files, reducing the final image size from 1.16 GB to just 22 MB.
Docker is a platform that lets developers and system administrators build, run, and share applications in containers. Containers run as isolated processes with their own filesystem built from Docker images defined by Dockerfiles.
The terms “dockerization” or “containerization” refer to the process of creating Docker containers. Containers are popular because they offer flexibility, lightweight nature, portability, loose coupling, and security.
This article focuses on optimizing Docker images to make them lightweight.
Example: Containerizing a React application
After creating a React app with TypeScript using npx create-react-app app --template typescript, the initial Dockerfile 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 ./buildThe resulting image size is shown in the following figure:
Step 1: Use a lightweight base image
Switching to an Alpine‑based image reduces the 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 ./buildThis optimization brings the image size down to 330 MB, as illustrated above.
Step 2: Multi‑stage build
By separating the build and runtime stages, the image size drops to 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
COPY --from=build /app/build ./build
EXPOSE 3000
CMD webserver.local -d ./buildStep 3: Use Nginx for serving static files
Replacing the Node runtime with Nginx further reduces the final image to 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 resulting container runs the React application successfully, as shown in the final figure:
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
