Setting Up a Docker-Hosted Droplet on DigitalOcean Using Terraform: A Step-by-Step Guide

·

3 min read

Welcome to the world of cloud infrastructure, where ease of deployment and scalability are paramount. Today, we're going to walk through setting up a Droplet (a virtual server) on DigitalOcean, one of the most user-friendly cloud platforms. Our focus will be on hosting Docker containers within this Droplet, all orchestrated using Terraform, a popular infrastructure as code tool.

Prerequisites

Before we start, ensure you have the following:

  1. A DigitalOcean Account: Sign up here if you haven’t already.

  2. Terraform Installed: Follow the installation guide.

  3. Docker Knowledge: Basic understanding of Docker and containerization.

  4. DigitalOcean API Token: Generate this from your DigitalOcean account for Terraform to interact with DigitalOcean's API.

Step 1: Setting Up Terraform

First, create a directory for your Terraform project:

mkdir terraform-digitalocean-droplet
cd terraform-digitalocean-droplet

Create a file named main.tf. This file will define your Terraform configuration.

provider "digitalocean" {
  token = "your_digitalocean_api_token"
}

Replace your_digitalocean_api_token with your actual API token.

Step 2: Defining the Droplet in Terraform

Now, define your Droplet resource in the main.tf file:

resource "digitalocean_droplet" "docker_host" {
  image  = "docker-18-04"
  name   = "docker-droplet"
  region = "nyc3"
  size   = "s-1vcpu-1gb"
}

This configuration creates a Droplet named docker-droplet in the NYC3 region with a basic size. The image is set to docker-18-04, a DigitalOcean image preconfigured with Docker.

Step 3: Creating a Volume for Docker

To store Docker data, let's add a volume to our Droplet. In main.tf, add:

resource "digitalocean_volume" "docker_volume" {
  region      = "nyc3"
  name        = "docker-volume"
  size        = 10
  initial_filesystem_type = "ext4"
  description = "Volume for Docker containers"
}

resource "digitalocean_volume_attachment" "docker_volume_attachment" {
  droplet_id = digitalocean_droplet.docker_host.id
  volume_id   = digitalocean_volume.docker_volume.id
}

This code creates a 10GB volume and attaches it to our Droplet.

Step 4: Initializing and Applying Terraform Configuration

Initialize your Terraform setup:

terraform init

Then, apply the configuration:

terraform apply

Confirm the plan by typing yes when prompted. Terraform will now create your Droplet and attach the volume.

Step 5: Accessing and Using Your Droplet

Once Terraform completes, use the Droplet's IP address to access it:

ssh root@your_droplet_ip

Check if Docker is running:

docker info

Your Docker-hosted Droplet is ready! You can now deploy containers on this Droplet.

Step 6: Mounting the Volume in Docker

Mount the attached volume in your Docker containers for persistent storage:

docker run -v /path/to/volume:/path/in/container -d your_image

Replace /path/to/volume with the actual path where your volume is mounted on the Droplet, and /path/in/container with the path inside your container where you want the volume to be mounted.

Conclusion

Congratulations! You've successfully set up a Docker-hosted Droplet on DigitalOcean using Terraform. This setup not only simplifies management and deployment of your infrastructure but also leverages the power of Docker for containerization, ensuring a scalable and efficient environment.