DigitalOcean
beginner
15-20 minutes
Installing doctl CLI
Install and configure the DigitalOcean CLI for command-line management of your cloud resources.
What you'll accomplish
doctl CLI installed, authenticated, and ready for deployments
Prerequisites
- DigitalOcean account created
- API token generated
What is doctl?
doctl is DigitalOcean's official command-line interface:
bash# Create a Droplet doctl compute droplet create myserver --image ubuntu-22-04-x64 --size s-1vcpu-1gb # Deploy an app doctl apps create --spec app.yaml # Manage databases doctl databases create mydb --engine pg
Why Use doctl?
- Automation - Script your infrastructure
- Speed - Faster than the web UI
- CI/CD - Integrate with pipelines
- Consistency - Same commands everywhere
Installation
Authentication
Step 1: Get Your API Token
- Go to cloud.digitalocean.com/account/api/tokens
- Click Generate New Token
- Name it (e.g., "CLI Access")
- Select Read and Write
- Copy the token
Step 2: Authenticate
bashdoctl auth init
Paste your API token when prompted.
Verify Authentication
bash# Check account doctl account get # Output: # Email Droplet Limit Email Verified UUID Status # [email protected] 25 true xxx active
Using Environment Variable
bash# Set in your shell profile export DIGITALOCEAN_ACCESS_TOKEN="your-token-here" # Or pass directly doctl compute droplet list --access-token "your-token"
Multiple Contexts
Manage multiple accounts:
bash# Add a context doctl auth init --context work doctl auth init --context personal # List contexts doctl auth list # Switch context doctl auth switch --context work
Output Formats
bash# Table format (default) doctl compute droplet list # JSON format doctl compute droplet list --output json # Text format (for scripting) doctl compute droplet list --format "ID,Name,PublicIPv4" --no-header
Common Commands
Account
bashdoctl account get # Account info doctl account ratelimit # API rate limits doctl balance get # Current balance
Droplets (Virtual Machines)
bashdoctl compute droplet list # List Droplets doctl compute droplet create # Create Droplet doctl compute droplet delete ID # Delete Droplet doctl compute droplet-action # Power on/off, reboot
App Platform
bashdoctl apps list # List apps doctl apps create --spec app.yaml # Create app doctl apps update APP_ID --spec # Update app doctl apps create-deployment APP_ID # Trigger deploy doctl apps logs APP_ID # View logs
Databases
bashdoctl databases list # List databases doctl databases create # Create database doctl databases connection DB_ID # Get connection string
Networking
bashdoctl compute domain list # List domains doctl compute domain create # Add domain doctl compute firewall list # List firewalls doctl compute load-balancer list # List load balancers
SSH Keys
bashdoctl compute ssh-key list # List SSH keys doctl compute ssh-key create # Add SSH key doctl compute ssh ID # SSH into Droplet
Droplet Management
Create Droplet
bash# Basic Droplet doctl compute droplet create myserver \ --image ubuntu-22-04-x64 \ --size s-1vcpu-1gb \ --region nyc1 # With SSH key doctl compute droplet create myserver \ --image ubuntu-22-04-x64 \ --size s-1vcpu-1gb \ --region nyc1 \ --ssh-keys $(doctl compute ssh-key list --format ID --no-header | head -1) # With user data script doctl compute droplet create myserver \ --image ubuntu-22-04-x64 \ --size s-1vcpu-1gb \ --region nyc1 \ --user-data-file ./startup.sh
Available Images
bash# List available images doctl compute image list --public # Common images: # ubuntu-22-04-x64 # ubuntu-24-04-x64 # debian-12-x64 # centos-stream-9-x64 # docker-20-04
Available Sizes
bash# List sizes doctl compute size list # Common sizes: # s-1vcpu-512mb-10gb - $4/mo # s-1vcpu-1gb - $6/mo # s-1vcpu-2gb - $12/mo # s-2vcpu-2gb - $18/mo # s-2vcpu-4gb - $24/mo
SSH into Droplet
bash# SSH by Droplet ID doctl compute ssh DROPLET_ID # SSH by name doctl compute ssh myserver
Droplet Actions
bash# Power off doctl compute droplet-action power-off DROPLET_ID # Power on doctl compute droplet-action power-on DROPLET_ID # Reboot doctl compute droplet-action reboot DROPLET_ID # Resize doctl compute droplet-action resize DROPLET_ID --size s-2vcpu-2gb # Snapshot doctl compute droplet-action snapshot DROPLET_ID --snapshot-name "backup-$(date +%Y%m%d)"
App Platform Commands
Create App from Spec
bashdoctl apps create --spec .do/app.yaml
List Apps
bashdoctl apps list
Get App Details
bashdoctl apps get APP_ID
Trigger Deployment
bashdoctl apps create-deployment APP_ID
View Logs
bash# Real-time logs doctl apps logs APP_ID --follow # Build logs doctl apps logs APP_ID --type BUILD # Deploy logs doctl apps logs APP_ID --type DEPLOY
Update App Spec
bashdoctl apps update APP_ID --spec .do/app.yaml
Database Commands
Create Database
bash# PostgreSQL doctl databases create mydb \ --engine pg \ --size db-s-1vcpu-1gb \ --region nyc1 \ --num-nodes 1 # MySQL doctl databases create mydb \ --engine mysql \ --size db-s-1vcpu-1gb # Redis doctl databases create myredis \ --engine redis \ --size db-s-1vcpu-1gb
Get Connection Info
bashdoctl databases connection DB_ID # Output includes: # - Host # - Port # - User # - Password # - Database name # - Connection URI
Manage Users
bash# Create user doctl databases user create DB_ID myuser # List users doctl databases user list DB_ID
Scripting Examples
Create Complete Stack
bash#!/bin/bash set -e # Configuration PROJECT_NAME="myapp" REGION="nyc1" # Create database echo "Creating database..." DB_ID=$(doctl databases create ${PROJECT_NAME}-db \ --engine pg \ --size db-s-1vcpu-1gb \ --region $REGION \ --output json | jq -r '.[0].id') echo "Database ID: $DB_ID" # Wait for database to be ready echo "Waiting for database..." while true; do STATUS=$(doctl databases get $DB_ID --format Status --no-header) if [ "$STATUS" = "online" ]; then break fi sleep 10 done # Get connection string CONNECTION_URI=$(doctl databases connection $DB_ID --format URI --no-header) echo "Connection: $CONNECTION_URI" # Create app with database cat > app.yaml << EOF name: $PROJECT_NAME region: $REGION services: - name: web github: repo: username/myapp branch: main envs: - key: DATABASE_URL value: "$CONNECTION_URI" type: SECRET EOF doctl apps create --spec app.yaml echo "Stack created successfully!"
Backup All Droplets
bash#!/bin/bash # Get all Droplet IDs DROPLETS=$(doctl compute droplet list --format ID --no-header) for ID in $DROPLETS; do NAME=$(doctl compute droplet get $ID --format Name --no-header) SNAPSHOT_NAME="${NAME}-backup-$(date +%Y%m%d)" echo "Creating snapshot: $SNAPSHOT_NAME" doctl compute droplet-action snapshot $ID --snapshot-name "$SNAPSHOT_NAME" done
Shell Completion
bash# Bash doctl completion bash > /etc/bash_completion.d/doctl # Zsh doctl completion zsh > "${fpath[1]}/_doctl" # Fish doctl completion fish > ~/.config/fish/completions/doctl.fish
Troubleshooting
"Unable to authenticate"
bash# Re-authenticate doctl auth init # Check token doctl account get
"Rate limit exceeded"
bash# Check rate limits doctl account ratelimit # Wait and retry, or use caching
"Command not found"
bash# Check installation which doctl # Add to PATH export PATH=$PATH:/usr/local/bin
Debug Mode
bash# Enable verbose output doctl --verbose compute droplet list
Note
Checkpoint: Run doctl account get to verify you're authenticated and connected to your DigitalOcean account.