Skip to content

Instantly share code, notes, and snippets.

@ivanvanderbyl
Last active August 29, 2015 14:08
Show Gist options
  • Save ivanvanderbyl/f6b4c03d8181411c2258 to your computer and use it in GitHub Desktop.
Save ivanvanderbyl/f6b4c03d8181411c2258 to your computer and use it in GitHub Desktop.
Launching Docker 1.3 Droplets on DigitalOcean
#!/bin/bash
# Usage:
# bash ./create_droplet.sh 10 MY_TOKEN
MAX_DROPLETS=$1
TOKEN=$2
create_droplet() {
curl -X POST "https://api.digitalocean.com/v2/droplets" \
-d "{\"name\":\"docker-$1\",\"region\":\"nyc3\",\"size\":\"1GB\",\"image\":6882330}" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json"
}
if [[ ! $TOKEN ]]; then
echo "create_droplets.sh COUNT TOKEN"
exit 1
fi
if [[ ! $MAX_DROPLETS ]]; then
echo "create_droplets.sh COUNT TOKEN"
exit 1
fi
for (( i = 1; i <= $MAX_DROPLETS; i++ )); do
echo "---> Creating Droplet $i"
create_droplet $i
echo
echo " Done."
done

Launching Docker 1.3 Droplets on DigitalOcean

Step 1:

Generate a Personal Access Token from the "Apps & API" page on the DigitalOcean Control Panel.

You will need to give it a name, this can be anything you like, for example "Docker write key", and give it Read & Write scopes.

Your token should look something like this: bd2f85cb824c9cc8ac5739dec7ed83705fd6c800f752574f590c193afd2849aa

Remember to copy your token from this page before leaving as there's no way to retrieve it again later (for security reasons).

Step 2:

Next we'll use the API to create a Docker Droplet. To do this we'll be using the curl command from the command line.

Firstly, export your token so we can use it without needing to paste it in everytime:

export TOKEN=YOUR_TOKEN_HERE

Then create your droplet:

curl -X POST "https://api.digitalocean.com/v2/droplets" \
	-d '{"name":"docker-01","region":"nyc3","size":"1GB","image":6882330}' \
	-H "Authorization: Bearer $TOKEN" \
	-H "Content-Type: application/json" 

You should see a response like this:

{"droplet":{"id":2993991,"name":"docker-01","memory":1024,"vcpus":1,"disk":30,"locked":true,"status":"new","kernel":{"id":2217,"name":"Ubuntu 14.04 x64 vmlinuz-3.13.0-36-generic-docker-memlimit","version":"root=LABEL=DOROOT ro"},"created_at":"2014-10-28T14:07:00Z","features":["virtio"],"backup_ids":[],"snapshot_ids":[],"image":{},"size_slug":"1gb","networks":{},"region":{}},"links":{"actions":[{"id":35452896,"rel":"create","href":"https://api.digitalocean.com/v2/actions/35452896"}]}}

And receive an email containing the root password. If you would like to specify an SSH key during this process, take a look at the SSH Keys API documentation

Step 3 (optional):

Automating this process to create n Droplets is easy with bash. Here's an example shell script to generate as many droplets as you need:

#!/bin/bash

# Usage:
# bash ./create_droplet.sh 10 MY_TOKEN

MAX_DROPLETS=$1
TOKEN=$2

create_droplet() {
  curl -X POST "https://api.digitalocean.com/v2/droplets" \
    -d "{\"name\":\"docker-$1\",\"region\":\"nyc3\",\"size\":\"1GB\",\"image\":6882330}" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json"
}

if [[ ! $TOKEN ]]; then
  echo "create_droplets.sh COUNT TOKEN"
  exit 1
fi

if [[ ! $MAX_DROPLETS ]]; then
  echo "create_droplets.sh COUNT TOKEN"
  exit 1
fi

for (( i = 1; i <= $MAX_DROPLETS; i++ )); do
  echo "---> Creating Droplet $i"
  create_droplet $i
  echo
  echo "     Done."
done

Run this with bash ./create_droplet.sh 300 $TOKEN to spin up 300 Docker Droplets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment