In this article we will be discussing how to install and use doctl to interact with your cloud resources on DigitalOcean cloud platform. doctl is the official DigitalOcean command line interface (CLI).

doctl

The doctl command line tool enables you to interact with the DigitalOcean API via the command line. It supports most functionality found in the control panel. You can create, configure, and destroy DigitalOcean resources like Droplets, Kubernetes clusters, firewalls, load balancers, database clusters, domains, and more.

In other articles we will cover creation and deletion of resources and applications deployed on DigitalOcean cloud platform.

Step 1: Install doctl on macOS and Linux

There are many ways from which doctl can be installed on macOS and Linux distributions.

Install doctl on macOS using Homebrew:

brew install doctl

Install doctl on Arch Linux

sudo pacman -S doctl

Install doctl on all other Linux distributions from binary files:

Download archive file.

# 64 bit Linux OS
curl -s https://api.github.com/repos/digitalocean/doctl/releases/latest | grep browser_download_url | cut -d '"' -f 4  | grep linux-amd64 | wget -qi -

# 32 bit Linux system
curl -s https://api.github.com/repos/digitalocean/doctl/releases/latest | grep browser_download_url | cut -d '"' -f 4  | grep linux-386 | wget -qi -

Once downloaded extract the file using the command below.

# 64-bit
tar xvf doctl-*-linux-amd64.tar.gz

# 32-bit
tar xvf doctl-*-linux-386.tar.gz

Move extracted binary file to /usr/local/bin directory

sudo mv doctl /usr/local/bin

Confirm doctl command is available in your bash shell and executable.

$ doctl version
doctl version 1.54.0-release
Git commit hash: a901a72

Step 2: Generate DigitalOcean Access Token

The DigitalOcean API allows you to manage Droplets and resources within the DigitalOcean cloud in a simple, programmatic way using conventional HTTP requests. The endpoints are intuitive and powerful, allowing you to easily make calls to retrieve information or to execute actions.

To generate the token login to DigitalOcean portal and head over to API > Applications & API Tokens/Keys > Generate New Token.

Step 3: Authenticating with DigitalOcean

Copy Token ID generated in step 2 and initiate the authentication with DigitalOcean by running the following command in your terminal. Enter DigitalOcean access token when prompted.

$ doctl auth init
Please authenticate doctl for use with your DigitalOcean account. You can generate a token in the control panel at https://cloud.digitalocean.com/account/api/tokens

Enter your access token:
Validating token... OK

The commands will initialize doctl to use a specific account. A confirmation message will be given once the credentials are accepted. If the token doesn’t validate, make sure you copied and pasted it correctly.

Validating token: OK

The default config file will be located in:

  • macOS${HOME}/Library/Application Support/doctl/config.yaml
  • Linux${XDG_CONFIG_HOME}/doctl/config.yaml
  • Windows: %APPDATA%\doctl\config.yaml

Confirm doctl is working by retrieving your API usage and the remaining quota:

$ doctl account ratelimit
Limit    Remaining    Reset
5000     4997         2021-01-04 11:33:09 +0300 EAT

Step 4: Doctl usage examples

Listing billing histories:

$ doctl billing-history list

Retrieving your account balance

$ doctl balance get
Month-to-date Balance    Account Balance    Month-to-date Usage    Generated At
8.49                     0.00               8.49                   2021-01-04T06:02:07Z

List running droplets

$ doctl compute droplet list

# Filter output
$ doctl compute droplet list --format Name,Memory,VCPUs,Disk,PrivateIPv4,PublicIPv4,Image,Status,Region

List available distribution images

$ doctl compute image list-distribution
ID          Name                 Type        Distribution    Slug                    Public    Min Disk
54203610    16.04.6 (LTS) x32    snapshot    Ubuntu          ubuntu-16-04-x32        true      20
59416024    12.1 ufs x64         snapshot    FreeBSD         freebsd-12-x64          true      20
65416372    v1.5.6               snapshot    RancherOS       rancheros               true      20
69440038    10 x64               snapshot    Debian          debian-10-x64           true      15
69440042    9 x64                snapshot    Debian          debian-9-x64            true      15
69452245    11.4 zfs x64         snapshot    FreeBSD         freebsd-11-x64-zfs      true      15
69500386    11.4 ufs x64         snapshot    FreeBSD         freebsd-11-x64-ufs      true      15
69535713    7.6 x64              snapshot    CentOS          centos-7-x64            true      20
70639049    32 x64               snapshot    Fedora          fedora-32-x64           true      15
72061309    18.04 (LTS) x64      snapshot    Ubuntu          ubuntu-18-04-x64        true      15
72067660    20.04 (LTS) x64      snapshot    Ubuntu          ubuntu-20-04-x64        true      15
72067667    16.04 (LTS) x64      snapshot    Ubuntu          ubuntu-16-04-x64        true      15
72181180    20.10 x64            snapshot    Ubuntu          ubuntu-20-10-x64        true      15
72465092    33 x64               snapshot    Fedora          fedora-33-x64           true      15
72791873    12.2 ufs x64         snapshot    FreeBSD         freebsd-12-x64-ufs      true      20
72794028    12.2 zfs x64         snapshot    FreeBSD         freebsd-12-x64-zfs      true      15
72855737    12.1 ufs x64         snapshot    FreeBSD         freebsd-12-1-x64-ufs    true      20
72903235    12.1 zfs x64         snapshot    FreeBSD         freebsd-12-1-x64-zfs    true      20
74885442    8.3 x64              snapshot    CentOS          centos-8-x64            true      15

When doing automation of droplets creation you’ll often use the image slug names.

To all list images on your account including backups use:

$ doctl compute image list

List your database clusters:

$ doctl databases list

Delete Droplet

$ doctl compute droplet delete <droplet-id|droplet-name>... [flags]

# Example
$ doctl compute droplet delete -f freebsd12

List available compute sizes

$ doctl compute size list

List valid compute resources regions

$ doctl compute region list
Slug    Name               Available
nyc1    New York 1         true
sfo1    San Francisco 1    false
nyc2    New York 2         false
ams2    Amsterdam 2        false
sgp1    Singapore 1        true
lon1    London 1           true
nyc3    New York 3         true
ams3    Amsterdam 3        true
fra1    Frankfurt 1        true
tor1    Toronto 1          true
sfo2    San Francisco 2    true
blr1    Bangalore 1        true
sfo3    San Francisco 3    true

Create a droplet

$ doctl compute droplet create <droplet-name>... [flags]

# Examples
$ doctl compute droplet create --ssh-keys <key-fingerprint> --image freebsd-12-x64-zfs --size s-1vcpu-1gb --region nyc1 freebsd12

Delete droplet:

$ doctl compute droplet delete -f freebsd12

Other DigitalOcean guides available are:

How To Deploy Droplet on DigitalOcean With Terraform

How To Run Arch Linux Droplet on DigitalOcean

How To Run openSUSE instance on DigitalOcean

LEAVE A REPLY

Please enter your comment!
Please enter your name here