What is Docker?
Docker is a containerization platform that packages applications and their dependencies into standardized units called containers. Containers are lightweight, isolated environments that ensure your application runs the same way on your laptop, server, or cloud platform.
Instead of virtualizing entire operating systems like VMs, Docker containers share the host OS kernel while maintaining isolation. This makes them faster, more efficient, and perfect for microservices architecture.
Why Use Docker?
- ✓ Consistency: Works identically across dev, test, and production
- ✓ Isolation: Containers are isolated from each other and the host
- ✓ Efficiency: Lightweight compared to VMs, faster startup
- ✓ Scalability: Easy to scale with container orchestration
- ✓ Microservices: Perfect for breaking monoliths into services
- ✓ Dependency Management: All dependencies packaged inside
Core Docker Concepts
Images
Blueprints for containers. Read-only templates containing your application code, runtime, libraries, and environment.
Think of it as a snapshot. Images can be versioned, shared, and layered.
Containers
Running instances of Docker images. Isolated processes with their own filesystem, network, and environment.
Multiple containers can run from the same image, each with independent data.
Registry
Central repositories for sharing Docker images. Docker Hub is the public default registry.
Can be public (shared with anyone) or private (restricted access).
Volumes
Persistent data storage for containers. Data survives container restarts.
Can be local, network-mounted, or cloud-based storage.
Networks
Allow containers to communicate with each other and external systems.
Multiple network types: bridge, host, overlay, macvlan.
Installing Docker
Linux Installation
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
macOS & Windows
Download Docker Desktop from docker.com. Includes Docker Engine, CLI, and Compose.
Verify installation: docker --version
Verify Installation
docker run hello-world
This pulls and runs a test image. Success message confirms proper installation.
Creating Dockerfiles
A Dockerfile is a text file with instructions to build a Docker image. Each instruction creates a new layer in the image.
Node.js Application Example
# Use official Node image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm install --production
# Copy application code
COPY . .
# Expose port
EXPOSE 3000
# Start application
CMD ["node", "server.js"]
Common Dockerfile Instructions
- FROM - Base image to build upon
- WORKDIR - Set working directory in container
- COPY/ADD - Copy files from host to container
- RUN - Execute commands (usually for installation)
- ENV - Set environment variables
- EXPOSE - Declare ports the container listens on
- CMD/ENTRYPOINT - Default command when container starts
Running Containers
docker build -t myapp:1.0 .
Build image from Dockerfile in current directory
docker run -d -p 3000:3000 --name mycontainer myapp:1.0
Run container in background, map ports, assign name
docker ps
List running containers
docker logs mycontainer
View container output and logs
docker stop mycontainer
Gracefully stop running container
docker rm mycontainer
Remove stopped container
Docker Compose
Define and run multi-container applications with Docker Compose. Single YAML file defines services, networks, and volumes.
docker-compose.yml Example
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://db/myapp
depends_on:
- db
db:
image: postgres:14
environment:
- POSTGRES_PASSWORD=secret
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Common Commands
docker-compose up - Start all services
docker-compose up -d - Start in background
docker-compose down - Stop and remove containers
docker-compose logs - View service logs
docker-compose exec web bash - Execute commands in service
Docker Best Practices
Use Specific Base Image Tags
Avoid 'latest' tag. Use specific versions: node:18.14-alpine
Minimize Image Size
Use multi-stage builds, Alpine base images, and remove unnecessary files.
Run as Non-Root User
Don't run containers as root. Create dedicated users for security.
Use .dockerignore
Exclude unnecessary files from build context like node_modules and .git
Keep Layers Minimal
Combine RUN commands to reduce layers and improve build performance.
Health Checks
Implement HEALTHCHECK to monitor container health and enable auto-restart.
Deploy Containers with Confidence
Host containerized applications with Spidey Host. Docker support, Kubernetes-ready, and automatic scaling included.
Get Container Hosting