Sep 22, 2025

How to build a DigitalOcean image with Packer?

Leave a message

In the dynamic landscape of cloud computing, DigitalOcean has emerged as a popular choice for developers and businesses alike, offering a user-friendly and cost-effective platform for hosting applications and services. Meanwhile, Packer, an open-source tool developed by HashiCorp, simplifies the process of creating identical machine images for multiple platforms from a single source configuration. As a Packer supplier, I am well-versed in the intricacies of leveraging Packer to build DigitalOcean images, and in this blog post, I will guide you through the step-by-step process.

Prerequisites

Before we dive into building a DigitalOcean image with Packer, there are a few prerequisites you need to have in place:

  1. Packer Installation: Ensure that Packer is installed on your local machine. You can download the latest version from the official Packer website and follow the installation instructions for your operating system.
  2. DigitalOcean Account: You need to have a DigitalOcean account. If you don't have one, you can sign up for free on the DigitalOcean website.
  3. DigitalOcean API Token: Generate a personal access token from your DigitalOcean account settings. This token will be used by Packer to interact with the DigitalOcean API.
  4. Basic Knowledge of JSON: Packer configurations are written in JSON format. Familiarize yourself with the basic syntax of JSON if you haven't already.

Understanding Packer Templates

Packer uses templates to define the build process. A template is a JSON file that contains one or more builders and provisioners. Builders are responsible for creating the machine image on a specific platform, while provisioners are used to install and configure software on the image.

Here is a basic structure of a Packer template for building a DigitalOcean image:

{
    "builders": [
        {
            "type": "digitalocean",
            "api_token": "{{user `do_api_token`}}",
            "image": "ubuntu-20-04-x64",
            "region": "nyc3",
            "size": "s-1vcpu-1gb",
            "ssh_username": "root",
            "snapshot_name": "my-custom-packer-image"
        }
    ],
    "provisioners": [
        {
            "type": "shell",
            "inline": [
                "apt-get update",
                "apt-get install -y nginx"
            ]
        }
    ]
}

In this example, the builder section specifies that we are using the DigitalOcean builder to create an image based on the Ubuntu 20.04 x64 distribution in the NYC3 region. The provisioner section uses the shell provisioner to update the package list and install Nginx on the image.

Step-by-Step Guide to Building a DigitalOcean Image with Packer

Step 1: Create a Packer Template

Create a new JSON file, for example, digitalocean-packer.json, and copy the above template into it. You can customize the template according to your requirements. For instance, you can change the base image, region, size, and the software packages to be installed.

Step 2: Set the API Token as an Environment Variable

To avoid hardcoding the DigitalOcean API token in the template, it is recommended to set it as an environment variable. Open your terminal and run the following command:

export DO_API_TOKEN=your_digitalocean_api_token

Step 3: Validate the Packer Template

Before running the build, it is a good practice to validate the template to ensure that it is syntactically correct. Run the following command in the terminal:

packer validate -var "do_api_token=$DO_API_TOKEN" digitalocean-packer.json

If the template is valid, Packer will output a success message.

Step 4: Build the Image

Once the template is validated, you can start the build process. Run the following command:

e3046b517e9c4a9e885d9117c9f7d894Retrievable Packer Systems

packer build -var "do_api_token=$DO_API_TOKEN" digitalocean-packer.json

Packer will now create a new Droplet on DigitalOcean, run the provisioning scripts, and then create a snapshot of the Droplet, which will be saved as a custom image.

Step 5: Verify the Image

After the build process is complete, log in to your DigitalOcean account and navigate to the Images section. You should see your custom image listed under the "Snapshots" tab.

Advanced Configuration and Customization

Using Multiple Provisioners

You can use multiple provisioners in a Packer template to perform different tasks. For example, you can use a file provisioner to copy files to the image and a shell provisioner to run scripts.

{
    "builders": [
        {
            "type": "digitalocean",
            "api_token": "{{user `do_api_token`}}",
            "image": "ubuntu-20-04-x64",
            "region": "nyc3",
            "size": "s-1vcpu-1gb",
            "ssh_username": "root",
            "snapshot_name": "my-custom-packer-image"
        }
    ],
    "provisioners": [
        {
            "type": "file",
            "source": "my-config-file.conf",
            "destination": "/etc/my-config-file.conf"
        },
        {
            "type": "shell",
            "inline": [
                "apt-get update",
                "apt-get install -y nginx",
                "cp /etc/my-config-file.conf /etc/nginx/sites-available/default",
                "systemctl restart nginx"
            ]
        }
    ]
}

Using Variables

Packer allows you to use variables in your templates to make them more flexible. You can define variables in the template or pass them as command-line arguments.

{
    "variables": {
        "do_api_token": "",
        "base_image": "ubuntu-20-04-x64",
        "region": "nyc3",
        "size": "s-1vcpu-1gb"
    },
    "builders": [
        {
            "type": "digitalocean",
            "api_token": "{{user `do_api_token`}}",
            "image": "{{user `base_image`}}",
            "region": "{{user `region`}}",
            "size": "{{user `size`}}",
            "ssh_username": "root",
            "snapshot_name": "my-custom-packer-image"
        }
    ],
    "provisioners": [
        {
            "type": "shell",
            "inline": [
                "apt-get update",
                "apt-get install -y nginx"
            ]
        }
    ]
}

Our Packer Offerings

As a Packer supplier, we offer a wide range of Retrievable Packer Systems that are designed to meet the diverse needs of our customers. Our Large Bore Double Grip Retrievable Packer is a high-quality product that provides reliable performance in challenging environments. We also have the Retrievable Packer 1, which is known for its ease of use and durability.

Contact Us for Procurement

If you are interested in our Packer products or need assistance with building DigitalOcean images using Packer, we encourage you to reach out to us for a procurement discussion. Our team of experts is ready to help you find the best solutions for your specific requirements.

References

  • HashiCorp Packer Documentation
  • DigitalOcean API Documentation
Send Inquiry