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.

Bobby Iliev
Site Moderator
Site Moderator badge
March 28, 2020
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

I wrote a script that is similar to Bobby’s but I used cloud-init to provide the script to execute to the droplet.

The only downside to this approach is that you still need a machine running locally to either make the SSH connection, or to monitor the droplet to figure out when it’s done. If that local machine goes away (e.g. needs to be rebooted), or your locally running script errors out (the one that monitors the droplet), or whatever, the droplet will sit there until you go manually remove it. In my case, the last thing my script does is shut down the droplet, so I just poll every 30 seconds to see if the droplet is powered on and if it is not, I destroy it.

What would really be nice is if there was an “ephemeral” droplet option, where you could instruct DO to automatically destroy the droplet when it is powered off. Much like the --rm option on a Docker container. Could be a parameter in the create API for example. Then you could just use cloud-init to provide the script to run.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel