Skip to content

123usermariya/reactapp-docker-image-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reduce Docker image size

This repository demonstrates practical steps I used to reduce the size of Docker images for a Node.js application. The goal is to keep images small, secure, and fast to build and deploy.


Summary

In this repo I applied three main strategies:

  1. Exclude unnecessary files via .dockerignore (e.g. node_modules, .git).
  2. Use a smaller base image (Alpine-based runtime) where appropriate.
  3. Multistage builds to compile/build in a larger builder image but copy only the runtime artifacts into the final, minimal image.

These changes together often reduce image size by a large factor and speed up CI/CD and deployments.


What I changed (high level)

  • Added a .dockerignore file that excludes node_modules/, .git/, local environment files, editor files, and other build artifacts so they are never sent to the Docker daemon.

  • Switched the final image to a small base (Alpine-based) to shrink the runtime image footprint.

  • Converted the Dockerfile to a multistage build:

    • builder stage uses a full Node image to install dependencies and build the app.
    • runner (final) stage uses a minimal image and only copies the production build and runtime dependencies.

Key steps used

  • Added a .dockerignore file to exclude unnecessary items such as node_modules, .git, local environment files, build outputs, and editor/system files.
  • Switched to a lightweight Alpine-based image to reduce the final runtime size.
  • Implemented multi‑stage builds so that dependencies and build tools exist only in the build stage, and only the final compiled output and production dependencies are included.

Extra tips

  • Install production‑only dependencies.
  • Remove package manager caches and temporary files.
  • Use minimal base images or distroless when appropriate.
  • Use caching properly to speed up builds.

What I validated (how to reproduce)

  1. Build the image locally: docker build -t myapp:latest .
  2. Check size: docker images | grep myapp.
  3. Inspect layers: docker history myapp:latest or use dive myapp:latest.

Result

The final Docker image is significantly smaller, faster to build, and optimized for production deployment.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors