containerization January 12, 2026 2 min read

Docker Fundamentals

A beginner-friendly guide to Docker — containers, images, Dockerfiles, and Docker Compose for modern development workflows.

Docker Fundamentals

Docker enables you to package applications and their dependencies into portable containers that run consistently across any environment.

Core Concepts

  • Image: Blueprint/template for a container
  • Container: Running instance of an image
  • Dockerfile: Instructions to build an image
  • Registry: Storage for images (Docker Hub)

Basic Commands

# Pull an image
docker pull node:18-alpine

# Run a container
docker run -d -p 3000:3000 --name myapp node:18-alpine

# List running containers
docker ps

# View logs
docker logs myapp

# Stop and remove
docker stop myapp
docker rm myapp

Dockerfile

# Use Node.js base image
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy source code
COPY . .

# Expose port
EXPOSE 3000

# Start the application
CMD ["node", "server.js"]

Docker Compose

version: '3.8'
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://db:5432/mydb
    depends_on:
      - db
  
  db:
    image: postgres:15-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: mydb
      POSTGRES_PASSWORD: secret

volumes:
  pgdata:

Best Practices

  1. Use multi-stage builds to reduce image size
  2. Use .dockerignore to exclude unnecessary files
  3. Run as non-root user for security
  4. Use specific tags (not latest) for reproducibility
  5. Layer caching: Order instructions from least to most frequently changing

Related Articles