VOOZH about

URL: https://dev.to/17j/day-6-docker-containerization-3cdb

⇱ Day 6 β€” Docker & Containerization - DEV Community


Modern software development has changed completely.

Applications are no longer deployed directly on servers like before.

Today, companies package applications into containers so they can run consistently anywhere:

  • Developer laptop
  • Cloud servers
  • Kubernetes clusters
  • CI/CD pipelines
  • Edge infrastructure

πŸ”— Resources


🧠 What is a Container?

A container is a lightweight isolated environment that includes:

  • Application code
  • Runtime
  • Libraries
  • Dependencies
  • Environment variables
  • System tools

Everything required to run the application is packaged together.

That means:

  • Same behavior everywhere
  • Faster deployments
  • Portable infrastructure
  • Easy scaling

βš”οΈ Containers vs Virtual Machines

This is one of the most important concepts in DevOps.

Both Containers and VMs isolate applications β€” but in very different ways.


πŸ–₯️ Virtual Machines (VMs)

A VM includes:

  • Full Operating System
  • Guest Kernel
  • Hypervisor
  • Application

Problems with VMs

  • High memory usage
  • Slow startup time
  • Large storage consumption
  • Less efficient scaling

🐳 Containers

πŸ‘ Container

Containers share the host OS kernel.

They only package:

  • Application
  • Dependencies
  • Runtime

This makes them:

  • Lightweight
  • Fast
  • Portable
  • Efficient

πŸ“Š Containers vs VMs Comparison

Feature Containers Virtual Machines
Startup Time Seconds Minutes
Size MBs GBs
Performance Near-native Heavy overhead
Isolation Process-level Full OS-level
Resource Usage Low High
Portability Excellent Moderate

πŸ“¦ What is Docker?

Docker is a platform used to:

  • Build applications
  • Package applications
  • Ship applications
  • Run applications in isolated environments called containers

Docker ensures:

β€œIt works the same everywhere.”

This solves the classic developer problem:

β€œIt works on my machine.”


πŸ—οΈ Docker Architecture

Docker mainly consists of 3 components:

πŸ‘ Docker Registry


1️⃣ Docker Client

The CLI where commands are executed.

Example:

docker build
docker run
docker ps

2️⃣ Docker Daemon

The background service (dockerd) responsible for managing:

  • Containers
  • Images
  • Networks
  • Volumes

3️⃣ Docker Registry

A place where Docker images are stored.

Popular registries include:

  • Docker Hub
  • GitHub Container Registry
  • AWS ECR
  • Google Artifact Registry

🧱 What is a Docker Image?

A Docker Image is a:

Read-only blueprint used to create containers.

It contains:

  • Application code
  • Dependencies
  • Runtime
  • Configuration
  • Libraries

🧠 Simple Analogy

Concept Real World Example
Docker Image Recipe
Docker Container Cooked Food

The image is the template.

The container is the running instance.


πŸ” Popular Docker Images

Some commonly used images:

nginx
ubuntu
node
python
mysql
redis
postgres

Pull an image:

docker pull nginx

Run a container:

docker run nginx

πŸ“ What is a Dockerfile?

A Dockerfile is a text file containing instructions used to build a Docker image.

This is where containerization begins.


🧱 Example Dockerfile

FROM node:20-alpine

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm ci --only=production

COPY . .

EXPOSE 3000

CMD ["node", "app.js"]

πŸ”¬ Dockerfile Explained


FROM

Defines the base image.

FROM node:20-alpine

WORKDIR

Sets the working directory inside the container.

WORKDIR /app

COPY

Copies files from local system into the container.

COPY . .

RUN

Executes commands during image build.

RUN npm install

EXPOSE

Documents which port the application uses.

EXPOSE 3000

CMD

Defines the default startup command.

CMD ["node", "app.js"]

πŸš€ Building Docker Images

Build image:

docker build -t myapp .

Explanation

Part Meaning
docker build Build Docker image
-t Tag image
myapp Image name
. Current directory

▢️ Running Containers

Run container:

docker run -p 3000:3000 myapp

πŸ” Understanding Port Mapping

-p 3000:3000

Means:

Host Machine Container
3000 3000

Access app on:

http://localhost:3000

πŸ“‚ Containerizing a Node.js Application


πŸ“ Project Structure

project/
β”‚
β”œβ”€β”€ app.js
β”œβ”€β”€ package.json
└── Dockerfile

πŸ“„ app.js

const express = require("express");
const app = express();

app.get("/", (req, res) => {
 res.send("Docker is working!");
});

app.listen(3000);

πŸ“„ package.json

{"dependencies":{"express":"^4.18.2"}}

πŸ“„ Dockerfile

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY . .

EXPOSE 3000

CMD ["node", "app.js"]

πŸ—οΈ Build the Image

docker build -t node-docker-demo .

▢️ Run the Container

docker run -p 3000:3000 node-docker-demo

Now open:

http://localhost:3000

You should see:

Docker is working!

πŸ“¦ Docker Image Layers

Docker images are layered.

Each instruction creates a new layer.

Example:

FROM ubuntu:24.04
RUN apt update
RUN apt install nginx
COPY . .

Each line becomes a cached layer.

This makes builds:

  • Faster
  • Efficient
  • Reusable

⚑ Docker Caching

Docker rebuilds only changed layers.

That’s why Dockerfiles should be optimized carefully.


βœ… Cache-Friendly Pattern

COPY package.json .
RUN npm install

COPY . .

Why?

Dependencies are cached separately.

If only source code changes, Docker skips reinstalling packages.


❌ Bad Pattern

COPY . .
RUN npm install

Any file change invalidates cache.

Build becomes slower.


πŸ“ Docker Volumes

Containers are ephemeral.

Data disappears after container removal.

Volumes solve this problem.


Create Volume

docker volume create mydata

Use volume:

docker run -v mydata:/data nginx

🌐 Docker Networking

Containers communicate using Docker networks.

Create network:

docker network create mynetwork

Run containers inside same network.

Useful for:

  • APIs
  • Databases
  • Microservices
  • Internal communication

🧹 Essential Docker Commands


πŸ“¦ Images

docker images
docker pull nginx
docker build -t myapp .
docker push username/myapp:v1
docker rmi image_id

🐳 Containers

docker ps
docker ps -a
docker stop container_id
docker rm container_id
docker logs -f container_id
docker exec -it container_id sh

🧽 Cleanup

docker system prune
docker volume prune

πŸ”₯ Why Docker Became So Popular

Docker transformed software deployment because it provides:

  • Environment consistency
  • Faster deployments
  • Infrastructure portability
  • Easy CI/CD integration
  • Better scalability
  • Microservices support
  • Cloud-native compatibility

☁️ Docker in Modern DevOps

Docker is now everywhere.

Technology Docker Usage
Kubernetes Runs containers
CI/CD Build & deploy
Cloud Platforms Container hosting
DevSecOps Isolated workloads
Microservices Service packaging

πŸ” Docker Security Basics

Important security practices:

  • Use minimal base images
  • Avoid running as root
  • Scan images regularly
  • Keep dependencies updated
  • Use signed images
  • Remove unused containers/images

🏭 Real-World Docker & K8s Workflow

πŸ‘ Docker WorkFlow


πŸš€ Docker Best Practices


βœ… Use Smaller Images

Prefer:

FROM node:20-alpine

Instead of huge base images.

Smaller images:

  • Pull faster
  • Reduce attack surface
  • Save storage

βœ… Always Use .dockerignore

Example:

node_modules
.git
.env
*.log

This reduces build size and prevents accidental secret leaks.


βœ… Multi-Stage Builds

Use one stage for building.

Use another minimal stage for production.


βœ… Tag Images Properly

Bad:

latest

Good:

v1.0.2

βœ… Don’t Run Containers as Root

Example:

RUN adduser -S appuser
USER appuser

🎭 Multi-Stage Build Example

# Stage 1 β€” Build
FROMnode:20-alpineASbuilder

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY . .

RUN npm run build

# Stage 2 β€” Production
FROM node:20-alpine

WORKDIR /app

COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules

EXPOSE 3000

CMD ["node", "dist/index.js"]

πŸ“š Final Thoughts

Docker completely changed how modern applications are built and deployed.

Understanding:

  • Containers
  • Docker Images
  • Dockerfiles
  • Layers
  • Volumes
  • Networking

is now a fundamental engineering skill.
Because modern infrastructure runs on containers.

most popular alternatives to Docker for containerization and container runtime workflows:

  • Podman
  • containerd
  • CRI-O

🎯 Quick Recap

Concept Meaning
Container Isolated runtime
Docker Image Blueprint/template
Dockerfile Build instructions
Container Running image instance
Volume Persistent storage
Network Container communication