Tonight I want to simply add Docker to my droplet by default. I ask ChatGpt about it. I've gotten my instance up and running. I would like to work on Master Plan step Infrastructure Setup: Install Docker on the instance to manage containers. What options are available to automatically install Docker into an Ubuntu 22.10 droplet at creation time?

To automatically install Docker on an Ubuntu 22.10 droplet at creation
time, you have a few options:

1. **User Data Script:** You can utilize the "User Data" feature provided
   by DigitalOcean. This allows you to run custom scripts during droplet
   creation. You can provide a shell script or a cloud-init configuration
   that installs Docker on the droplet. Here's an example of a cloud-init
   configuration to install Docker:

   #cloud-config
   packages:
     - docker.io
   runcmd:
     - systemctl enable --now docker

   You can provide this cloud-init configuration in the "User Data" field
   when creating the droplet. DigitalOcean will automatically execute the
   script during the droplet's initialization process, ensuring Docker is
   installed and running.

2. **Droplet Image:** You can create a custom droplet image with Docker 
   pre-installed. This involves creating a base droplet, installing Docker,
   and then taking a snapshot of the droplet. You can then use this custom
   image to create new droplets that already have Docker installed.
   DigitalOcean provides an option to create custom images from existing
   droplets or snapshots.

3. **Configuration Management Tools:** If you are using a configuration
   management tool like Ansible, Puppet, or Chef, you can create a
   configuration script that installs Docker and use the tool to provision
   the droplet. These tools provide a way to automate the setup and
   configuration of software on servers.

These options allow you to automate the installation of Docker on your Ubuntu
22.10 droplet during the creation process. Choose the option that best fits
your workflow and infrastructure setup.

An interesting thing to notice is that I’m telling ChatGPT about the work I’ve done outside of my conversation. Does this make a difference? Not sure, but it’s how I do things for some reason.

I also don’t 100% trust ChatGPT after yesterday’s results. I do a quick search through the documentation about cloud-config. It’s a good thing I did. Stack Overflow suggests a slightly different approach to getting Docker installed.

#cloud-config

packages:
  - docker.io

# create the docker group
groups:
  - docker

# Add default auto created user to docker group
system_info:
  default_user:
    groups: [docker]

I add this to my digitalocean_create.py script. In the process I add a loop to wait for activation of the droplet, and then print out the IPv4 address. This saves me a couple of steps. All of this comes from reading the package documentation & some trial and error. Then, as I do this I spot a note in the documentation about Manager.get_all_sshkeys() as well and use that to remove a hard coded value. Which gives me the following script.

import digitalocean
import os
from time import sleep

# Set up your Digital Ocean API token
api_token = os.environ.get('DIGITALOCEAN_APIKEY')

# Create a Droplet instance
manager = digitalocean.Manager(token=api_token)

# Get the SSH keys
sshKeys = manager.get_all_sshkeys()

# Define the Cloud-Init script
cloudConfig = '''
#cloud-config

packages:
  - docker.io

# create the docker group
groups:
  - docker

# Add default auto created user to docker group
system_info:
  default_user:
    groups: [docker]
'''

# Define the parameters for creating the droplet
droplet_params = {
    'name': 'ubuntu-s-1vcpu-1gb-tor1-01',  # Set your desired droplet name
    'region': 'tor1',  # Set your desired region
    'size': 's-1vcpu-1gb',  # Set the droplet size
    'image': 'ubuntu-22-04-x64',  # Set the droplet image
    'ssh_keys': sshKeys,  # Set the droplet SSH keys
    'backups': False,
    'ipv6': False,
    'user_data': None,
    'private_networking': None,
    'volumes': None,
    'user_data': cloudConfig
}

# Create the droplet
droplet = digitalocean.Droplet(token=api_token, **droplet_params)
droplet.create()

print(f"Waiting for droplet to become active...")
while True:
    droplet.load()
    print(droplet.status)
    if droplet.status == "active":
        break
    sleep(10)

print(f"Created droplet with ID: {droplet.id}")
print(f"Droplet IP: {droplet.networks['v4'][0]['ip_address']}")

The output looks like this

Waiting for droplet to become active...
new
new
new
new
active
Created droplet with ID: 364943395
Droplet IP: 137.184.162.133

A quick ssh session into the instance confirms that Docker is installed and running. Now I just need to put a basic app in Docker.