DEV Community

Cover image for From 1.57GB to 189MB: How I Slashed My Docker Image Size by 88%
saheed
saheed

Posted on

From 1.57GB to 189MB: How I Slashed My Docker Image Size by 88%

INTRODUCTION

As developers, we all know the pain of bloated Docker images slowing down deployments and burning through cloud resources. I recently tackled this head-on with my fullstack e-commerce payment platform. What started as a simple Node.js app ballooned into a lesson in DevOps efficiency slashing image sizes by 88% while automating everything from code commits to AWS deployments.

If you're into containerization, CI/CD, or just want to see how small tweaks can yield massive gains, stick around. I'll walk you through the project, the optimizations, and what I learned. The full repo is on GitHub feel free to star, fork, or drop feedback!
https://github.com/Saheed94/E-commerce-Application/tree/main

Project Overview

This is a containerized e-commerce payment system built with modern DevOps in mind. It features an interactive frontend integrated with a Node.js + Express REST API for product management and payments. The magic? Everything's automated: Infrastructure spins up via Terraform, and GitHub Actions handles CI/CD to deploy Docker images to AWS EC2.

Key demos include:
End-to-end automation: Commit code → Build & push Docker image → Deploy to live EC2.

Reusable IaC: Terraform modules for VPC, subnets, security groups, and more.

Secure Docker practices: Non-root containers and optimized builds.

Scalable releases: CI/CD workflows that make updates seamless.

Project Highlights

Full-Stack E-Commerce App: Frontend UI meets backend API for smooth product browsing and payments.

88% Docker Image Reduction: From a hefty 1.57GB to a lean 189MB using multi-stage builds.

Automated AWS Setup: Terraform provisions everything custom VPC, EC2, and security.

CI/CD Pipeline: GitHub Actions builds, pushes to Docker Hub, and deploys on dev branch commits.

Security First: Restricted access, least-privilege design, and health checks.

Quick Start
Ready to try it? Here's how:

Prerequisites
Node.js 18+
Docker Desktop
Git

Clone the Repo

git clone [](https://github.com/Saheed94/E-commerce-Application/tree/main
)
Enter fullscreen mode Exit fullscreen mode

Run Locally (No Docker)

npm install
npm start
Enter fullscreen mode Exit fullscreen mode

Open: http://localhost:3000/health

Run with Docker (Optimized)

docker build -t payment-api:optimized .
docker run -p 3000:3000 payment-api:optimized
Enter fullscreen mode Exit fullscreen mode

Open: http://localhost:3000/health

Compare Optimization

docker build -f Dockerfile.unoptimized -t payment-api:unoptimized .
docker images payment-api
Enter fullscreen mode Exit fullscreen mode

Expect: Optimized at 189MB vs. unoptimized at 1.57GB.

Testing

curl http://localhost:3000/health
curl http://localhost:3000/api/products
curl -X POST http://localhost:3000/api/payments \
  -H "Content-Type: application/json" \
  -d '{"productId":1,"quantity":1,"cardNumber":"4532015112830366","cvv":"123","expiryDate":"12/25","email":"test@example.com"}'
Enter fullscreen mode Exit fullscreen mode

Automated Script

chmod +x test-api.sh
./test-api.sh
Enter fullscreen mode Exit fullscreen mode

Docker Optimization Deep Dive

Unoptimized Docker Script (1.57GB)

Problems: Heavy base image, all deps included, poor caching, root user.
Optimized Multi-Stage (189MB)

Alpine base, multi-stage discards junk, prod-only deps, caching, non-root.

What I Learned

Multi-stage + Alpine = game changers for size and speed.

Security Matters: No root.

DevOps Flow: Image size affects costs; logging/monitoring are musts.

Documentation: As crucial as code!

What do you think? Have you optimized Docker images before? Share your tips below, or check out the repo and let me know how to improve. Let's connect on GitHub or here on Dev.to!

CONCLUSION

This project taught me that optimization isn't optional—it's table stakes for production systems. The techniques (multi-stage, Alpine, non-root, caching) apply to every Dockerized app, not just e-commerce APIs.

Top comments (3)

Collapse
 
ijay profile image
Ijay

Nice write-up 🙌....

Collapse
 
saheed_ea3f3e90be19db2eac profile image
saheed

Thank you so much

Collapse
 
tricsusz profile image
Tóth Richárd

Thanks saheed, nice article!