Question

Can I add initialization scripts to droplet that is already deployed?

I want an environment variable that specifies the Droplet’s domain name to be set at boot even if the VM’s image is reinstalled. Do I need to destroy this droplet and create a new one, or can I modify my existing droplet?


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
September 4, 2023

Hi there,

Yes, you can modify an already deployed Droplet without destroying it or re-create it.

This could be done in a few different ways depending on the exact requirements, but here are a few examples:

  1. Use /etc/environment: This is a system-wide configuration file to be used by all processes. You can append your environment variable here:

    echo 'DOMAIN_NAME="yourdomain.com"' | sudo tee -a /etc/environment
    

    Remember to load the environment after setting:

    source /etc/environment
    
  2. Startup Scripts with systemd: You can use systemd to create custom startup services that run your script at boot:

    • Create a script, for example, /usr/local/bin/set-domain.sh:

      #!/bin/bash
      export DOMAIN_NAME="yourdomain.com"
      
    • Make the script executable: chmod +x /usr/local/bin/set-domain.sh

    • Create a systemd service unit for this script, for example, /etc/systemd/system/set-domain.service:

      [Unit]
      Description=Set domain name environment variable
      
      [Service] ExecStart=/usr/local/bin/set-domain.sh
      
      [Install]
      WantedBy=multi-user.target
      
    • Enable and start the service:

      systemctl enable set-domain
      systemctl start set-domain
      

Alternatively, if you’ve used cloud-init for your initial setup, you can modify the cloud-init configuration. However, changing cloud-init scripts on an already running Droplet can be tricky since cloud-init is primarily designed for the initial setup.

Hope that this helps!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
September 4, 2023

Heya,

I’ll recommend you to check the cloud-init stuff - https://docs.digitalocean.com/tutorials/droplet-cloudinit/

Cloud-init is an industry standard tool that allows you to automate the initialization of your Linux instances. This means that you can use cloud-init to inject a file into your Droplets at deployment that automatically sets up things like new users, firewall rules, app installations, and SSH keys. DigitalOcean’s User Data feature leverages cloud-init so you can deploy and automate the set up of several Droplets simultaneously. Learning to use cloud-init can save you a lot of set up time when deploying new Droplets.

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