Why Docker on a VPS?
Docker on a VPS gives you the perfect balance: container isolation with predictable costs. Unlike managed container services, you pay a fixed monthly fee regardless of traffic spikes.
Prerequisites
VPS Requirements:
• Minimum 1 GB RAM (2 GB recommended)
• Ubuntu 22.04+ or Debian 12+
• Root/sudo access
Step 1: Install Docker
Using the official Docker installation script (documentation ↗):
# Update system
sudo apt update && sudo apt upgrade -y
# Install Docker using official script
curl -fsSL https://get.docker.com | sh
# Add your user to docker group
sudo usermod -aG docker $USER
# Log out and back in, then verify
docker --version
Step 2: Install Docker Compose
# Install Docker Compose plugin
sudo apt install docker-compose-plugin
# Verify installation
docker compose version
Step 3: Basic Deployment Example
Create a docker-compose.yml file:
version: "3.8"
services:
app:
image: nginx:alpine
ports:
- "80:80"
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:
Step 4: Production Best Practices
🔒 Security
Use .env files for secrets. Never commit passwords to version control.
📊 Logging
Configure log rotation to prevent disk fill:logging:
driver: json-file
options:
max-size: "10m"
🔄 Restart Policy
Always use restart: unless-stopped for production services.
Resource Usage Guidelines
| Container Type | Typical RAM | Recommended VPS |
|---|---|---|
| Nginx/Static | 20-50 MB | 1 GB tier |
| Node.js App | 100-300 MB | 2 GB tier |
| PostgreSQL | 200-500 MB | 2-4 GB tier |
| Redis | 50-200 MB | 1-2 GB tier |
Common Issues & Solutions
- Container won't start: Check logs with
docker logs container_name - Out of disk space: Prune unused images:
docker system prune -a - Port conflict: Check what's using the port:
sudo lsof -i :80