Back to all articles
September 2, 20269 min readJaykumar Kanjia

Production CI/CD with GitHub Actions: Multi-Stage Docker Builds, Layer Caching & Docker Hub Push for FastAPI & Node.js

Step-by-step blueprint for building ultra-fast, minimal production Docker images for FastAPI (Python) and Node.js using multi-stage builds, leveraging GitHub Actions gha cache backend, automated Docker Hub image publishing, and vulnerability auditing with Trivy.

GitHub Actions
Docker
CI/CD
FastAPI
Node.js
Production CI/CD with GitHub Actions: Multi-Stage Docker Builds, Layer Caching & Docker Hub Push for FastAPI & Node.js cover

Optimizing Container CI/CD Pipelines for High Velocity

Standard Docker builds in CI environments frequently download and rebuild layers from scratch, causing pipeline runtimes to exceed 10-15 minutes. By implementing Docker Buildx with GitHub Actions inline caching (type=gha) and multi-stage container optimization, build times drop to under 90 seconds.

1. Multi-Stage Dockerfile Architecture for Node.js & FastAPI

For Node.js / Next.js, splitting dependencies installation, TypeScript compilation, and standalone output bundling reduces image footprint from 1.2 GB to ~110 MB on Alpine. For FastAPI, using a python:3.12-slim builder stage to compile wheels and installing them into a distroless runtime eliminates GCC and dev headers from the attack surface.

2. GitHub Actions Matrix Build & Docker Hub Secret Authentication

Our reusable GitHub Actions workflow uses matrix jobs to build and test both backend services concurrently. Images are tagged with Git commit SHA and semantic versioning, verified with Trivy vulnerability scanning, and pushed securely to Docker Hub before dispatching deployment webhooks.

Jaykumar Kanjia
Written by

Jaykumar Kanjia

Sr. Software Engineer specializing in scalable backend architectures, high-performance Redis caching, and autonomous AI agent workflows.