Introduction
In the previous article, we learned:
Frontend deployment
Backend hosting
Cloud PostgreSQL databases
Environment variables
Production builds
Domain configuration
CI/CD basics
Now it is time to move toward modern cloud-native development using:
Docker
Modern applications are often deployed using containers because they make applications:
Portable
Scalable
Consistent
Easier to deploy
In this article, we will learn:
What Docker is
What containers are
Docker images
Dockerfile
Docker Compose
Containerizing PERN applications
Multi-container architecture
Kubernetes basics
What is Docker?
Docker is a platform used to package applications into containers.
What is a Container?
A container is:
A lightweight isolated environment for running applications
It includes:
Application code
Dependencies
Libraries
Runtime environment
Why Containers are Important
Without containers:
Applications may work on one system
Fail on another system
This causes:
Environment Problems
Docker solves this issue.
Real-World Example
Suppose your application works on:
Windows
but fails on:
Linux Server
Docker ensures identical environments everywhere.
What is a Docker Image?
A Docker image is:
A blueprint for creating containers
It contains:
Application code
Packages
Configurations
What is a Docker Container?
A container is:
A running instance of a Docker image
Docker Workflow
Dockerfile
↓
Docker Image
↓
Docker Container
Installing Docker
Install Docker Desktop from:
Checking Docker Installation
Run:
docker --version
Example output:
Docker version 27.0.0
Understanding Dockerfile
A:
Dockerfile
contains instructions for creating Docker images.
Backend Dockerfile Example
Inside backend folder create:
Dockerfile
Example Dockerfile
FROM node:20
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["npm", "start"]
Understanding Dockerfile Instructions
| Instruction | Purpose |
|---|---|
| FROM | Base image |
| WORKDIR | Working directory |
| COPY | Copy files |
| RUN | Execute commands |
| EXPOSE | Open port |
| CMD | Start application |
Understanding Base Images
FROM node:20
uses official Node.js image.
Building Docker Image
Run inside backend folder:
docker build -t pern-backend .
Understanding docker build
| Part | Meaning |
|---|---|
| docker build | Build image |
| -t | Image tag |
| pern-backend | Image name |
| . | Current directory |
Running Docker Container
docker run -p 5000:5000 pern-backend
Understanding Port Mapping
-p 5000:5000
means:
Local Port : Container Port
Viewing Running Containers
docker ps
Stopping Containers
docker stop container_id
What is Docker Compose?
Docker Compose manages multiple containers together.
Why Docker Compose is Important
A PERN stack application needs:
React container
Express container
PostgreSQL container
Managing them individually becomes difficult.
Docker Compose solves this problem.
Creating docker-compose.yml
In project root:
docker-compose.yml
Example Docker Compose File
version: '3'
services:
backend:
build: ./server
ports:
- "5000:5000"
frontend:
build: ./client
ports:
- "3000:3000"
database:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: pern_app
ports:
- "5432:5432"
Understanding Services
| Service | Purpose |
|---|---|
| backend | Express API |
| frontend | React app |
| database | PostgreSQL |
Starting Docker Compose
Run:
docker-compose up
Running in Background
docker-compose up -d
Stopping Docker Compose
docker-compose down
Environment Variables in Docker
Docker Compose supports:
.env
files.
Example:
POSTGRES_PASSWORD=password123
JWT_SECRET=mysecret
Accessing Variables in Compose
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
Docker Volumes
Containers normally lose data after removal.
Volumes provide:
Persistent Storage
PostgreSQL Volume Example
volumes:
- pgdata:/var/lib/postgresql/data
Why Volumes are Important
Without volumes:
Database data gets deleted
Uploaded files disappear
Named Volumes
volumes:
pgdata:
Docker Networking
Docker Compose automatically creates internal networks.
Containers communicate using:
Service Names
Example Database Connection
Instead of:
localhost
use:
database
inside Docker containers.
Backend Database Example
host: 'database'
Multi-Container PERN Architecture
React Container
↓
Express Container
↓
PostgreSQL Container
Advantages of Docker
Portability
Runs identically everywhere.
Scalability
Easy to scale applications.
Consistency
Same environment for:
Developers
Testing
Production
Faster Deployment
Deployment becomes simpler.
What is Kubernetes?
Kubernetes manages large-scale containerized applications.
Why Kubernetes is Used
Kubernetes helps:
Scale containers
Handle failures
Load balance traffic
Manage deployments
Docker vs Kubernetes
| Docker | Kubernetes |
|---|---|
| Creates containers | Manages containers |
| Single application | Large infrastructure |
What is a Pod?
In Kubernetes:
Pod
is the smallest deployable unit.
Kubernetes Features
Kubernetes provides:
Auto scaling
Self-healing
Load balancing
Rolling updates
Real-World Companies Using Docker
Docker is used by:
Netflix
Spotify
PayPal
Amazon
Google
Common Beginner Mistakes
Not Using .dockerignore
Create:
.dockerignore
Example
node_modules
.env
Copying node_modules
Never copy:
node_modules
inside images.
Hardcoding Secrets
Use environment variables instead.
Using Large Images
Use smaller base images when possible.
Best Practices
Use Multi-Stage Builds
Reduces image size.
Keep Containers Small
Smaller containers:
Start faster
Use less memory
Use Docker Compose
For multi-container applications.
Use Volumes for Databases
Prevents data loss.
Real-World DevOps Workflow
Modern workflow:
Code
↓
GitHub
↓
CI/CD Pipeline
↓
Docker Build
↓
Cloud Deployment
Conclusion
You have now learned Docker and containerization in the PERN stack.
You learned:
Docker images
Containers
Dockerfile
Docker Compose
Multi-container architecture
Volumes
Networking
Kubernetes basics
Now your PERN stack applications are becoming cloud-native and production-ready.
Comments
Post a Comment