Create Droplet to run single task then destroy


I have a function in my workflow that does some video encoding and needs a CPU optimized droplet to run efficiently.

The thing is I only need it to run a few times a day. So I wanted a way to initiate a task that creates a droplet (possibly a docker container), has it run a task and then destroy it.

I know I can create a cheap droplet and code it with the DigitalOcean API to accept tasks, create a droplet, run the task and then destroy it but I was wondering if there was already something built out that I can use to not reinvent the wheel.

Any help or hints in the right direction would be helpful. Thank you!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hi there @mindglitch,

I don’t think that there is built-in functionality to do that via the DigitalOcean control panel directly. Though I think that this is a great idea, sounds like it’d be super useful!

The best thing to do to get your voice heard regarding this would be to head over to our Product Ideas board and post a new idea, including as much information as possible for what you’d like to see implemented.

On another note, I have a small bash script that I use for similar tasks.

NOTE: Please, carefully review the whole script before using it.

The prerequisites are:

What the script does is:

  • Creates a new Droplet with the details that you’ve specified
  • Waits for the Droplet to become active
  • Runs a key scan
  • Executes a set of commands on the new Droplet over SSH
  • Deletes the Droplet

# Variables
# Add the details of the Droplet that you would like to create


# Droplet info session file
temp_droplet=$(mktemp /tmp/temp-droplet.XXXXXX)

# Create Droplet
function new_droplet() {
    echo "Creating Droplet with the following details:"
    echo "Droplet name: ${droplet_name}"
    echo "Droplet size: ${droplet_size}"
    echo "Droplet image: ${droplet_image}"
    echo "Droplet regon: ${droplet_region}"

    doctl compute droplet create ${droplet_name} --size ${droplet_size} --image ${droplet_image} --region ${droplet_region} --ssh-keys ${ssh_key_id} > ${temp_droplet}
    echo "Waiting 60 seconds for Droplet to be created..."
    sleep 1m

    new_droplet_id=$(cat ${temp_droplet} | tail -1 | awk '{ print $1 }')
    new_droplet_ip=$(doctl compute droplet get ${new_droplet_id} --template "{{ .PublicIPv4}}")
    echo "Droplet IP: ${new_droplet_ip}"

# Check if Droplet is running

function ssh_key_scan() {
    while [ "$status" != "active" ] ; do
        echo "Waiting for status to become active.."
        sleep 5
        status=$(doctl compute droplet get ${new_droplet_id} --template "{{ .Status}}")
    echo "Droplet status is: ${status}. Proceeding to SSH Key Scan"
    ssh-keyscan ${new_droplet_ip} >> ~/.ssh/known_hosts

# Execute a command

function run_commands() {
    echo "Running commands.."
    ssh root@${new_droplet_ip} 'cat /etc/os-release'

# Delete Droplet and session file
function clean_up(){
    echo "Deleting Droplet and temp files.."
    doctl compute droplet delete -f ${new_droplet_id}
    rm ${temp_droplet}

In the run_commands function you can change the command/commands which you would like to execute.

Hope that this helps! Regards, Bobby

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.