Cloud Native 6 min read

Optimizing Docker Images for Lightweight Deployment

By switching to a lightweight Alpine base, employing multi‑stage builds to separate compilation from runtime, and finally serving the React app with an Nginx Alpine image, Docker images can be reduced from over a gigabyte to roughly twenty‑two megabytes, enabling efficient, minimal‑footprint container deployment.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Optimizing Docker Images for Lightweight Deployment

Docker is a platform that allows developers and sysadmins to build, run, and share applications in containers. Containers run processes in isolated file systems built from Docker images defined by Dockerfiles.

Key advantages of containers include flexibility, lightweight nature, portability, loose coupling, and security.

Optimization Process

Step 1: Use a lightweight base image

Switching from the default node:10 image to node:10-alpine reduces the image size from 1.16 GB 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 2: Multi‑stage build

Separate the build environment from the runtime image, keeping only the compiled assets.

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

Resulting size: ~91.5 MB.

Step 3: Use Nginx for serving static files

Replace the Node runtime with an Nginx image, further shrinking 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 final container runs the React application efficiently with a minimal footprint.

DockerImage OptimizationContainerizationNginxAlpineMulti‑Stage Build
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

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.