Building a LAMP Stack with Terraform and Docker

Building a LAMP Stack with Terraform and Docker

·

2 min read

In this tutorial, we'll use Terraform to orchestrate the creation of a simple LAMP (Linux, Apache, MySQL, PHP) stack using Docker containers. We'll follow the given specifications to create Docker images, containers, and volumes required for the LAMP stack.

Prerequisites:

  • Terraform installed on your machine.

  • Docker installed and running.

  • Access to /root/code/terraform-challenges/challenge2 directory for storing Terraform configuration files.

Terraform Configuration:

Step 1: Create provider.tf File:

Create a file named provider.tf and add the following content:

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
    }
  }
}

provider "docker" {}

Step 2: Define Docker Image Resources:

Create a file named docker_images.tf and define the Docker image resources:

resource "docker_image" "php-httpd-image" {
  name         = "php-httpd:challenge"
  build        = {
    context    = "lamp_stack/php_httpd"
  }
  labels = {
    challenge = "second"
  }
}

resource "docker_image" "mariadb-image" {
  name         = "mariadb:challenge"
  build        = {
    context    = "lamp_stack/custom_db"
  }
  labels = {
    challenge = "second"
  }
}

Step 3: Define Docker Container Resources:

Create a file named docker_containers.tf and define the Docker container resources:

resource "docker_container" "php-httpd" {
  name         = "webserver"
  image        = docker_image.php-httpd-image.latest
  hostname     = "php-httpd"
  network_mode = "my_network"
  ports {
    internal = 80
    external = 80
  }
  labels = {
    challenge = "second"
  }
  volumes {
    host_path      = "/root/code/terraform-challenges/challenge2/lamp_stack/website_content/"
    container_path = "/var/www/html"
  }
}

resource "docker_container" "phpmyadmin" {
  name         = "db_dashboard"
  image        = "phpmyadmin/phpmyadmin"
  hostname     = "phpmyadmin"
  network_mode = "my_network"
  ports {
    internal = 80
    external = 8081
  }
  labels = {
    challenge = "second"
  }
  depends_on = [docker_container.mariadb]
}

resource "docker_container" "mariadb" {
  name         = "db"
  image        = docker_image.mariadb-image.latest
  hostname     = "db"
  network_mode = "my_network"
  ports {
    internal = 3306
    external = 3306
  }
  labels = {
    challenge = "second"
  }
  environment = {
    MYSQL_ROOT_PASSWORD = "1234"
    MYSQL_DATABASE      = "simple-website"
  }
  volumes {
    name      = "mariadb-volume"
    container = "/var/lib/mysql"
  }
}

resource "docker_volume" "mariadb_volume" {
  name   = "mariadb-volume"
  labels = {
    challenge = "second"
  }
  attach {
    name = docker_container.mariadb.name
  }
}

Step 4: Create Docker Network:

Create a file named docker_network.tf and define the Docker network resource:

resource "docker_network" "my_network" {
  name = "my_network"
}

Step 5: Initialize and Apply Terraform Configuration:

Initialize Terraform and apply the configuration:

terraform init
terraform apply

Conclusion:

In this tutorial, we used Terraform to automate the deployment of a LAMP stack using Docker containers. We defined Docker image, container, volume, and network resources to set up the necessary components for the stack. By following the provided steps, you can efficiently deploy and manage LAMP stacks in a consistent and reproducible manner using Terraform.