DigitalOcean CLI Commands (doctl)
Overview
DigitalOcean CLI (doctl) provides command-line access to DigitalOcean services including Kubernetes clusters, Container Registry, Spaces (object storage), and Droplets. This reference covers essential commands for AzmX infrastructure management.
Authentication and Setup
Installation
# Ubuntu/Debian
sudo snap install doctl
# macOS with Homebrew
brew install doctl
# Windows with Scoop
scoop install doctl
# Create alias for easier usage (if using snap)
echo "alias doctl='snap run doctl'" >> ~/.bash_aliases
source ~/.bash_aliases
Authentication Flow
graph LR
A[Generate API Token] --> B[doctl auth init]
B --> C[Enter API Token]
C --> D[Create Context]
D --> E[Verify Access]
E --> F[Switch Contexts]
style A fill:#4ecdc4
style D fill:#74b9ff
style E fill:#00b894
Context Management
# Initialize authentication with context
doctl auth init --context azmx-production
doctl auth init --context azmx-development
# Switch between contexts
doctl auth switch --context azmx-production
doctl auth switch --context azmx-development
# List available contexts
doctl auth list
# Remove old context
doctl auth remove --context old-context
# Display current context
doctl auth switch
Kubernetes (DOKS) Management
Cluster Operations
# List all clusters
doctl kubernetes cluster list
# Get cluster details
doctl kubernetes cluster get azmx-production-cluster
# Create new cluster
doctl kubernetes cluster create azmx-cluster \
--region nyc3 \
--version 1.28.2-do.0 \
--count 3 \
--size s-2vcpu-4gb
# Update cluster
doctl kubernetes cluster update azmx-cluster \
--count 5
# Delete cluster
doctl kubernetes cluster delete azmx-cluster
Node Pool Management
# List node pools
doctl kubernetes cluster node-pool list azmx-cluster
# Add node pool
doctl kubernetes cluster node-pool create azmx-cluster \
--name high-memory-pool \
--size s-4vcpu-8gb \
--count 2
# Scale node pool
doctl kubernetes cluster node-pool update azmx-cluster high-memory-pool \
--count 4
# Delete node pool
doctl kubernetes cluster node-pool delete azmx-cluster high-memory-pool
Kubeconfig Management
# Download kubeconfig
doctl kubernetes cluster kubeconfig save azmx-production-cluster
# Download to specific location
doctl kubernetes cluster kubeconfig save azmx-cluster \
--kubeconfig-path ~/.kube/azmx-config
# Remove kubeconfig
doctl kubernetes cluster kubeconfig remove azmx-cluster
# Set environment variable for kubectl
export KUBECONFIG=~/.kube/azmx-config
kubectl get nodes
Container Registry
Registry Management
# Create container registry
doctl registry create azmx-registry
# Login to registry
doctl registry login
# Get registry information
doctl registry get
# Delete registry
doctl registry delete azmx-registry
Image Operations
# List repositories
doctl registry repository list
# List repository tags
doctl registry repository list-tags azmx/backend-api
# Delete repository tag
doctl registry repository delete-tag azmx/backend-api v1.2.3
# Get repository manifest
doctl registry repository get-manifest azmx/backend-api v1.2.3
Docker Integration
# Login to DigitalOcean Container Registry
docker login registry.digitalocean.com
# Use API token as both username and password
# Tag and push image
docker tag backend-api:latest registry.digitalocean.com/azmx-registry/backend-api:v1.2.3
docker push registry.digitalocean.com/azmx-registry/backend-api:v1.2.3
# Pull image
docker pull registry.digitalocean.com/azmx-registry/backend-api:v1.2.3
# Logout from registry
doctl registry logout
Spaces (Object Storage)
Space Management
# List all spaces
doctl compute spaces list
# Create new space
doctl compute spaces create azmx-production-assets \
--region nyc3
# Delete space
doctl compute spaces delete azmx-production-assets
File Operations
# Upload single file
doctl compute spaces cp local-file.txt do:azmx-assets/uploads/
# Upload with custom permissions
doctl compute spaces cp local-file.txt do:azmx-assets/public/ \
--acl public-read
# Download file
doctl compute spaces cp do:azmx-assets/uploads/file.txt local-file.txt
# List files in space
doctl compute spaces ls do:azmx-assets/
# List files recursively
doctl compute spaces ls do:azmx-assets/ --recursive
# Delete file
doctl compute spaces rm do:azmx-assets/uploads/old-file.txt
Directory Synchronization
# Sync local directory to space
doctl compute spaces sync local-directory/ do:azmx-assets/backup/
# Sync with delete (removes files not in source)
doctl compute spaces sync local-directory/ do:azmx-assets/backup/ --delete
# Sync from space to local
doctl compute spaces sync do:azmx-assets/backup/ local-directory/
# Dry run to see what would be synced
doctl compute spaces sync local-directory/ do:azmx-assets/backup/ --dryrun
Droplet Management
Droplet Operations
# List droplets
doctl compute droplet list
# Create droplet
doctl compute droplet create azmx-worker-01 \
--region nyc3 \
--image ubuntu-22-04-x64 \
--size s-2vcpu-4gb \
--ssh-keys "ssh-key-fingerprint"
# Get droplet information
doctl compute droplet get 123456789
# Resize droplet
doctl compute droplet actions resize 123456789 \
--size s-4vcpu-8gb \
--disk
# Power operations
doctl compute droplet actions power-on 123456789
doctl compute droplet actions power-off 123456789
doctl compute droplet actions reboot 123456789
# Delete droplet
doctl compute droplet delete 123456789
SSH Key Management
# List SSH keys
doctl compute ssh-key list
# Add SSH key
doctl compute ssh-key create "My Key" \
--public-key-file ~/.ssh/id_rsa.pub
# Delete SSH key
doctl compute ssh-key delete ssh-key-fingerprint
Load Balancer Management
Load Balancer Operations
# List load balancers
doctl compute load-balancer list
# Create load balancer
doctl compute load-balancer create \
--name azmx-api-lb \
--region nyc3 \
--forwarding-rules "entry_protocol:https,entry_port:443,target_protocol:http,target_port:80" \
--droplet-ids "123456789,987654321"
# Add droplets to load balancer
doctl compute load-balancer add-droplets azmx-api-lb-id \
--droplet-ids "123456789"
# Remove droplets from load balancer
doctl compute load-balancer remove-droplets azmx-api-lb-id \
--droplet-ids "123456789"
# Delete load balancer
doctl compute load-balancer delete azmx-api-lb-id
Database Management
Database Cluster Operations
# List database clusters
doctl databases list
# Create database cluster
doctl databases create azmx-production-db \
--engine postgres \
--version 14 \
--region nyc3 \
--size db-s-2vcpu-4gb \
--num-nodes 1
# Get database cluster info
doctl databases get azmx-production-db
# Resize database cluster
doctl databases resize azmx-production-db \
--size db-s-4vcpu-8gb \
--num-nodes 2
# Delete database cluster
doctl databases delete azmx-production-db
Database User Management
# List database users
doctl databases user list azmx-production-db
# Create database user
doctl databases user create azmx-production-db \
--username app_user
# Reset user password
doctl databases user reset azmx-production-db app_user
# Delete database user
doctl databases user delete azmx-production-db app_user
Database Management
# List databases
doctl databases db list azmx-production-db
# Create database
doctl databases db create azmx-production-db app_database
# Delete database
doctl databases db delete azmx-production-db app_database
Monitoring and Alerts
Resource Monitoring
# Get droplet metrics
doctl monitoring metrics droplet cpu 123456789 \
--start 2025-01-01T00:00:00Z \
--end 2025-01-02T00:00:00Z
# Get load balancer metrics
doctl monitoring metrics load_balancer requests azmx-api-lb-id \
--start 2025-01-01T00:00:00Z \
--end 2025-01-02T00:00:00Z
Alert Policies
# List alert policies
doctl monitoring alert list
# Create alert policy
doctl monitoring alert create \
--type droplet \
--description "High CPU usage alert" \
--compare GreaterThan \
--value 80 \
--window 5m \
--entities "123456789"
# Delete alert policy
doctl monitoring alert delete alert-policy-uuid
Troubleshooting
Common Issues
Authentication Problems
# Check current authentication
doctl auth list
# Re-authenticate
doctl auth init --context current-context
# Verify API token permissions
doctl account get
Network Issues
# Test API connectivity
curl -X GET \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
"https://api.digitalocean.com/v2/account"
# Check region availability
doctl compute region list
# Verify resource limits
doctl account ratelimit
Performance Optimization
# Use output format for scripting
doctl compute droplet list --format "ID,Name,PublicIPv4,Status"
# Filter results
doctl compute droplet list --tag-name production
# Use JSON output for processing
doctl compute droplet list --output json | jq '.droplets[].name'
Automation and Scripting
Bash Integration
#!/bin/bash
# Deploy application to Kubernetes
deploy_to_k8s() {
local context=$1
local image_tag=$2
echo "Switching to context: $context"
doctl auth switch --context $context
echo "Updating kubeconfig"
doctl kubernetes cluster kubeconfig save azmx-cluster
echo "Deploying image: $image_tag"
kubectl set image deployment/backend-api \
backend-api=registry.digitalocean.com/azmx-registry/backend-api:$image_tag
}
# Usage
deploy_to_k8s "azmx-production" "v1.2.3"
CI/CD Integration
# GitHub Actions example
- name: Deploy to DigitalOcean Kubernetes
run: |
echo "${{ secrets.DIGITALOCEAN_TOKEN }}" | doctl auth init --context github-actions
doctl kubernetes cluster kubeconfig save azmx-cluster
kubectl apply -f k8s/
Related Documents
- Container Registry Integration - Docker registry setup
Upcoming Documentation
- Kubernetes Setup - Detailed Kubernetes configuration (coming soon)
- Monitoring Setup - Comprehensive monitoring configuration (coming soon)
Last Updated: 2025-01-20 Tools Required: doctl CLI, kubectl, Docker Team Contacts: DevOps Lead for infrastructure management, Tech Lead for deployment automation