makeporngreatagain.pro
yeahporn.top
hd xxx

Working with Terraform for managing Docker

2,475
  1. Pre-requisite
    1. Create an EC2 – Please refer this link
    2. Install Docker – Please refer this link
    3. Install Terraform – Please refer this link
  2. Terraform Commands:
    1. List the Terraform commands:
      terraform
      Common commands:
      apply: Builds or changes infrastructure
      console: Interactive console for Terraform interpolations
      destroy: Destroys Terraform-managed infrastructure
      fmt: Rewrites configuration files to canonical format
      get: Downloads and installs modules for the configuration
      graph: Creates a visual graph of Terraform resources
      import: Imports existing infrastructure into Terraform
      init: Initializes a new or existing Terraform configuration
      output: Reads an output from a state file
      plan: Generates and shows an execution plan
      providers: Prints a tree of the providers used in the configuration
      push: Uploads this Terraform module to Terraform Enterprise to run
      refresh: Updates local state file against real resources
      show: Inspects Terraform state or plan
      taint: Manually marks a resource for recreation
      untaint: Manually unmarks a resource as tainted
      validate: Validates the Terraform files
      version: Prints the Terraform version
      workspace: Workspace management
    2. Set up the environment:
      mkdir -p terraform/basics
      cd terraform/basics
    3. Create a Terraform script:
      vi main.tf

      main.tf contents:

      # Download the latest Ghost image
      resource "docker_image" "image_id" {
      name = "ghost:latest"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "ghost_blog"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "2368"
          external = "80"
        }
      }
    4. Initialize Terraform:
      terraform init
    5. Validate the Terraform file:
      terraform validate
    6. List providers in the folder:
      ls .terraform/plugins/linux_amd64/
    7. List providers used in the configuration:
      terraform providers
    8. Terraform Plan:
      terraform plan

      Useful flags for plan:
      -out=path: Writes a plan file to the given path. This can be used as input to the “apply” command.
      -var 'foo=bar': Set a variable in the Terraform configuration. This flag can be set multiple times.

    9. Terraform Apply:
      terraform apply

      Useful flags for apply:
      -auto-approve: This skips interactive approval of plan before applying.
      -var 'foo=bar': This sets a variable in the Terraform configuration. It can be set multiple times.

      Confirm your apply by typing yes. The apply will take a bit to complete.

    10. List the Docker images:
      docker image ls
    11. Terraform Show:
      terraform show
    12. Terraform Destroy:
      terraform destroy

      Useful flags for destroys:
      -auto-approve: Skip interactive approval of plan before applying.
      Confirm your destroy by typing yes.

    13. Re-list the Docker images:
      docker image ls
    14. Using a plan:
      terraform plan -out=tfplan
    15. Applying a plan:
      terraform apply tfplan
    16. Show the Docker Image resource:
      terraform show
    17. Destroy the resource once again:
      terraform destroy
    18. Open Ghost blog using Docker IP
  3. Tainting and Updating Resources
    1. Tainting a resource:
      terraform taint docker_container.container_id

      Check tainted resource which will be recreated using

      terraform plan
    2. Untainting a resource:
      terraform untaint docker_container.container_id
    3. Let’s edit main.tf and change the image to ghost:alpine.
      vi main.tf

      replace the existing content with below one

      # Download the latest Ghost image
      resource "docker_image" "image_id" {
        name = "ghost:alpine"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "ghost_blog"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "2368"
          external = "80"
        }
      }
    4. Now do a terraform validate, plan & apply
      terraform validate
      terraform plan
      terraform apply
    5. Check the new image id by using
      docker images ls
    6. Reset the environment:
      terraform destroy
      

      Confirm the destroy by typing yes.
      List the Docker images:

      docker image ls
      

      Remove the Ghost blog image:

      docker image rm ghost:latest
      

      Reset main.tf:

      vi main.tf
      

      main.tf contents:

      # Download the latest Ghost image
      resource "docker_image" "image_id" {
        name = "ghost:latest"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "ghost_blog"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "2368"
          external = "80"
        }
      }
  4. Terraform Console and Output
    1. Redeploy the Ghost image and container:
      terraform apply
      
    2. Show the Terraform resources:
      terraform show
      
    3. Start the Terraform console:
      terraform console
      
    4. Type the following in the console to get the container’s name:
      docker_container.container_id.name
      
    5. Type the following in the console to get the container’s IP:
      docker_container.container_id.ip_address
      

      Break out of the Terraform console by using Ctrl+C.

    6. Destroy the environment:
      terraform destroy
    7. Edit main.tf:
      vi main.tf
      

      main.tf contents:

      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
        name = "ghost:latest"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "blog"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "2368"
          external = "80"
        }
      }
      
      #Output the IP Address of the Container
      output "ip_address" {
        value       = "${docker_container.container_id.ip_address}"
        description = "The IP for the container."
      }
      
      #Output the Name of the Container
      output "container_name" {
        value       = "${docker_container.container_id.name}"
        description = "The name of the container."
      }
      
    8. Validate  & Apply changes:
      terraform validate
      terraform apply
      
    9. Reset the environment:
      terraform destroy
  5. Input Variables
    1. Edit main.tf:
      vi main.tf

      main.tf contents:

      #Define variables
      variable "image_name" {
        description = "Image for container."
        default     = "ghost:latest"
      }
      
      variable "container_name" {
        description = "Name of the container."
        default     = "blog"
      }
      
      variable "int_port" {
        description = "Internal port for container."
        default     = "2368"
      }
      
      variable "ext_port" {
        description = "External port for container."
        default     = "80"
      }
      
      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
        name = "${var.image_name}"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "${var.container_name}"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "${var.int_port}"
          external = "${var.ext_port}"
        }
      }
      
      #Output the IP Address of the Container
      output "ip_address" {
        value       = "${docker_container.container_id.ip_address}"
        description = "The IP for the container."
      }
      
      output "container_name" {
        value       = "${docker_container.container_id.name}"
        description = "The name of the container."
      }
    2. Validate the changes:
      terraform validate
    3. Plan the changes:
      terraform plan
    4. Apply the changes using a variable:
      terraform apply -var 'ext_port=8080'
    5. Change the container name:
      terraform apply -var 'container_name=ghost_blog' -var 'ext_port=8080'
    6. Reset the environment:
      terraform destroy -var 'ext_port=8080'
  6. Breaking Out Our Variables and Outputs
    1. Edit variables.tf:
      vi variables.tf

      variables.tf contents:

      #Define variables
      variable "container_name" {
        description = "Name of the container."
        default     = "blog"
      }
      variable "image_name" {
        description = "Image for container."
        default     = "ghost:latest"
      }
      variable "int_port" {
        description = "Internal port for container."
        default     = "2368"
      }
      variable "ext_port" {
        description = "External port for container."
        default     = "80"
      }
    2. Edit main.tf:
      vi main.tf

      main.tf contents:

      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
        name = "${var.image_name}"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "${var.container_name}"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "${var.int_port}"
          external = "${var.ext_port}"
        }
      }
    3. Edit outputs.tf:
      vi outputs.tf

      outputs.tf contents:

      #Output the IP Address of the Container
      output "ip_address" {
        value       = "${docker_container.container_id.ip_address}"
        description = "The IP for the container."
      }
      
      output "container_name" {
        value       = "${docker_container.container_id.name}"
        description = "The name of the container."
      }
    4. Validate the changes:
      terraform validate
    5. Plan the changes:
      terraform plan -out=tfplan -var container_name=ghost_blog
    6. Apply the changes:
      terraform apply tfplan
    7. Destroy deployment:
      terraform destroy -auto-approve -var container_name=ghost_blog
  7. Maps and Lookups
    1. Edit variables.tf:
      vi variables.tf
      

      variables.tf contents:

      #Define variables
      variable "env" {
        description = "env: dev or prod"
      }
      variable "image_name" {
        type        = "map"
        description = "Image for container."
        default     = {
          dev  = "ghost:latest"
          prod = "ghost:alpine"
        }
      }
      
      variable "container_name" {
        type        = "map"
        description = "Name of the container."
        default     = {
          dev  = "blog_dev"
          prod = "blog_prod"
        }
      }
      
      variable "int_port" {
        description = "Internal port for container."
        default     = "2368"
      }
      variable "ext_port" {
        type        = "map"
        description = "External port for container."
        default     = {
          dev  = "8081"
          prod = "80"
        }
      }
      
    2. Validate the change:
      terraform validate
      
    3. Edit main.tf:
      vi main.tf
      

      main.tf contents:

      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
        name = "${lookup(var.image_name, var.env)}"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "${lookup(var.container_name, var.env)}"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "${var.int_port}"
          external = "${lookup(var.ext_port, var.env)}"
        }
      }
      
    4. Plan the dev deploy:
      terraform plan -out=tfdev_plan -var env=dev
      
    5. Apply the dev plan:
      terraform apply tfdev_plan
      
    6. Plan the prod deploy:
      terraform plan -out=tfprod_plan -var env=prod
      
    7. Apply the prod plan:
      terraform apply tfprod_plan
      
    8. Destroy prod deployment:
      terraform destroy -var env=prod -auto-approve
      
    9. Use environment variables:
      export TF_VAR_env=prod
      
    10. Open the Terraform console:
      terraform console
      
    11. Execute a lookup:
      lookup(var.ext_port, var.env)
      
    12. Exit the console:
      unset TF_VAR_env
  8. Workspaces
    1. Create a dev workspace:
      terraform workspace new dev
      
    2. Plan the dev deployment:
      terraform plan -out=tfdev_plan -var env=dev
      
    3. Apply the dev deployment:
      terraform apply tfdev_plan
      
    4. Change workspaces:
      terraform workspace new prod
      
    5. Plan the prod deployment:
      terraform plan -out=tfprod_plan -var env=prod
      
    6. Apply the prod deployment:
      terraform apply tfprod_plan
      
    7. Select the default workspace:
      terraform workspace select default
      
    8. Find what workspace we are using:
      terraform workspace show
      
    9. Select the dev workspace:
      terraform workspace select dev
      
    10. Destroy the dev deployment:
      terraform destroy -var env=dev
      
    11. Select the prod workspace:
      terraform workspace select prod
      
    12. Destroy the prod deployment:
      terraform destroy -var env=prod
  9. Null Resources and Local-exec
    1. Edit main.tf:
      vi main.tf

      main.tf contents:

      # Download the latest Ghost Image
      resource "docker_image" "image_id" {
        name = "${lookup(var.image_name, var.env)}"
      }
      
      # Start the Container
      resource "docker_container" "container_id" {
        name  = "${lookup(var.container_name, var.env)}"
        image = "${docker_image.image_id.latest}"
        ports {
          internal = "${var.int_port}"
          external = "${lookup(var.ext_port, var.env)}"
        }
      }
      
      resource "null_resource" "null_id" {
        provisioner "local-exec" {
          command = "echo ${docker_container.container_id.name}:${docker_container.container_id.ip_address} >> container.txt"
        }
      }
      
    2. Reinitialize Terraform:
      terraform init
      
    3. Validate the changes:
      terraform validate
      
    4. Plan the changes:
      terraform plan -out=tfplan -var env=dev
      
    5. Apply the changes:
      terraform apply tfplan
      
    6. View the contents of container.txt:
      cat container.txt
      
    7. Destroy the deployment:
      terraform destroy -auto-approve -var env=dev
  10. Terraform Modules
    1. Basic Module Directory Setup
      1. Set up the environment:
        mkdir -p modules/image
        mkdir -p modules/container
      2. Create files for the image:
        cd ~/terraform/basics/modules/image
        touch main.tf variables.tf outputs.tf
      3. Create files for container:
        cd ~/terraform/basics/modules/contain
    2. The Image Module
      1. Go to the image directory:
        cd ~/terraform/basics/modules/image
        
      2. Edit main.tf:
        vi main.tf
        

        main.tf contents:

        # Download the Image
        resource "docker_image" "image_id" {
          name = "${var.image_name}"
        }
        
      3. Edit variables.tf:
        vi variables.tf
        

        variables.tf contents:

        variable "image_name" {
          description = "Name of the image"
        }
        
      4. Edit outputs.tf:
        vi outputs.tf
        

        outputs.tf: contents:

        output "image_out" {
          value       = "${docker_image.image_id.latest}"
        }
        
      5. Initialize Terraform:
        terraform init
        
      6. Create the image plan:
        terraform plan -out=tfplan -var 'image_name=ghost:alpine'
        
      7. Deploy the image using the plan:
        terraform apply -auto-approve tfplan
        
      8. Destroy the image:
        terraform destroy -auto-approve -var 'image_name=ghost:alpine'
    3. The Container Module
      1. Go to the container directory:
        cd ~/terraform/basics/modules/container

         

      2. Edit main.tf:
        vi main.tf

        main.tf contents:

        # Start the Container
        resource "docker_container" "container_id" {
          name  = "${var.container_name}"
          image = "${var.image}"
          ports {
            internal = "${var.int_port}"
            external = "${var.ext_port}"
          }
        }
      3. Edit variables.tf:
        vi variables.tf

        variables.tf contents:

        #Define variables
        variable "container_name" {}
        variable "image" {}
        variable "int_port" {}
        variable "ext_port" {}
      4. Edit outputs.tf:
        vi outputs.tf

        outputs.tf contents:

        #Output the IP Address of the Container
        output "ip" {
          value = "${docker_container.container_id.ip_address}"
        }
        
        output "container_name" {
          value = "${docker_container.container_id.name}"
        }
      5. Initialize:
        terraform init
      6. Create the image plan:
        terraform plan -out=tfplan -var 'container_name=blog' -var 'image=ghost:alpine' -var 'int_port=2368' -var 'ext_port=80'
      7. Deploy container using the plan:
        terraform apply tfplan
    4. The Root Module
      1. Go to the module directory:
        cd ~/terraform/basics/modules/
        touch {main.tf,variables.tf,outputs.tf}
        
      2. Edit main.tf:
        vi main.tf
        

        main.tf contents:

        # Download the image
        module "image" {
          source = "./image"
          image_name  = "${var.image_name}"
        }
        
        # Start the container
        module "container" {
          source             = "./container"
          image              = "${module.image.image_out}"
          container_name     = "${var.container_name}"
          int_port           = "${var.int_port}"
          ext_port           = "${var.ext_port}"
        }
        
      3. Edit variables.tf:
        vi variables.tf
        

        variables.tf contents:

        #Define variables
        variable "container_name" {
          description = "Name of the container."
          default     = "blog"
        }
        variable "image_name" {
          description = "Image for container."
          default     = "ghost:latest"
        }
        variable "int_port" {
          description = "Internal port for container."
          default     = "2368"
        }
        variable "ext_port" {
          description = "External port for container."
          default     = "80"
        }
        
      4. Edit outputs.tf:
        vi outputs.tf
        

        outputs.tf contents:

        #Output the IP Address of the Container
        output "ip" {
          value = "${module.container.ip}"
        }
        
        output "container_name" {
          value = "${module.container.container_name}"
        }
        
      5. Initialize Terraform:
        terraform init
        
      6. Create the image plan:
        terraform plan -out=tfplan
        
      7. Deploy the container using the plan:
        terraform apply tfplan
        
      8. Destroy the deployment:
        terraform destroy -auto-approve
  11. Managing Docker Networks
    1. Set up the environment:
      mkdir -p ~/terraform/docker/networks
      cd terraform/docker/networks
      
    2. Create the files:
      touch {variables.tf,image.tf,network.tf,main.tf}
      
    3. Edit variables.tf:
      vi variables.tf
      

      variables.tf contents:

      variable "mysql_root_password" {
        description = "The MySQL root password."
        default     = "P4sSw0rd0!"
      }
      
      variable "ghost_db_username" {
        description = "Ghost blog database username."
        default     = "root"
      }
      
      variable "ghost_db_name" {
        description = "Ghost blog database name."
        default     = "ghost"
      }
      
      variable "mysql_network_alias" {
        description = "The network alias for MySQL."
        default     = "db"
      }
      
      variable "ghost_network_alias" {
        description = "The network alias for Ghost"
        default     = "ghost"
      }
      
      variable "ext_port" {
        description = "Public port for Ghost"
        default     = "8080"
      }
      
    4. Edit image.tf:
      vi image.tf
      

      image.tf contents:

      resource "docker_image" "ghost_image" {
        name = "ghost:alpine"
      }
      
      resource "docker_image" "mysql_image" {
        name = "mysql:5.7"
      }
      
    5. Edit network.tf:
      vi network.tf
      

      network.tf contents:

      resource "docker_network" "public_bridge_network" {
        name   = "public_ghost_network"
        driver = "bridge"
      }
      
      resource "docker_network" "private_bridge_network" {
        name     = "ghost_mysql_internal"
        driver   = "bridge"
        internal = true
      }
      
    6. Edit main.tf:
      vi main.tf
      

      main.tf contents:

      resource "docker_container" "blog_container" {
        name  = "ghost_blog"
        image = "${docker_image.ghost_image.name}"
        env   = [
          "database__client=mysql",
          "database__connection__host=${var.mysql_network_alias}",
          "database__connection__user=${var.ghost_db_username}",
          "database__connection__password=${var.mysql_root_password}",
          "database__connection__database=${var.ghost_db_name}"
        ]
        ports {
          internal = "2368"
          external = "${var.ext_port}"
        }
        networks_advanced {
          name    = "${docker_network.public_bridge_network.name}"
          aliases = ["${var.ghost_network_alias}"]
        }
        networks_advanced {
          name    = "${docker_network.private_bridge_network.name}"
          aliases = ["${var.ghost_network_alias}"]
        }
      }
      
      resource "docker_container" "mysql_container" {
        name  = "ghost_database"
        image = "${docker_image.mysql_image.name}"
        env   = [
          "MYSQL_ROOT_PASSWORD=${var.mysql_root_password}"
        ]
        networks_advanced {
          name    = "${docker_network.private_bridge_network.name}"
          aliases = ["${var.mysql_network_alias}"]
        }
      }
      
    7. Initialize Terraform:
      terraform init
      
    8. Validate the files:
      terraform validate
      
    9. Build a plan:
      terraform plan -out=tfplan -var 'ext_port=8082'
      
    10. Apply the plan:
      terraform apply tfplan
      
    11. Destroy the environment:
      terraform destroy -auto-approve -var 'ext_port=8082'
      
    12. Fixing main.tf

      main.tf contents:

      resource "docker_container" "mysql_container" {
        name  = "ghost_database"
        image = "${docker_image.mysql_image.name}"
        env   = [
          "MYSQL_ROOT_PASSWORD=${var.mysql_root_password}"
        ]
        networks_advanced {
          name    = "${docker_network.private_bridge_network.name}"
          aliases = ["${var.mysql_network_alias}"]
        }
      }
      
      resource "null_resource" "sleep" {
        depends_on = ["docker_container.mysql_container"]
        provisioner "local-exec" {
          command = "sleep 15s"
        }
      }
      
      resource "docker_container" "blog_container" {
        name  = "ghost_blog"
        image = "${docker_image.ghost_image.name}"
        depends_on = ["null_resource.sleep", "docker_container.mysql_container"]
        env   = [
          "database__client=mysql",
          "database__connection__host=${var.mysql_network_alias}",
          "database__connection__user=${var.ghost_db_username}",
          "database__connection__password=${var.mysql_root_password}",
          "database__connection__database=${var.ghost_db_name}"
        ]
        ports {
          internal = "2368"
          external = "${var.ext_port}"
        }
        networks_advanced {
          name    = "${docker_network.public_bridge_network.name}"
          aliases = ["${var.ghost_network_alias}"]
        }
        networks_advanced {
          name    = "${docker_network.private_bridge_network.name}"
          aliases = ["${var.ghost_network_alias}"]
        }
      }
      
    13. Build a plan:
      terraform plan -out=tfplan -var 'ext_port=8082'
      
    14. Apply the plan:
      terraform apply tfplan
  12. Managing Docker Volumes
    1. Setup an environment:
      cp -r ~/terraform/docker/networks ~/terraform/docker/volumes
      cd ../volumes/
    2. Create volumes.tf:
      vi volumes.tf

      volumes.tf contents:

      resource "docker_volume" "mysql_data_volume" {
        name = "mysql_data"
      }
    3. Edit main.tf:
      vi main.tf

      main.tf contents:

      resource "docker_container" "mysql_container" {
        name  = "ghost_database"
        image = "${docker_image.mysql_image.name}"
        env   = [
          "MYSQL_ROOT_PASSWORD=${var.mysql_root_password}"
        ]
        volumes {
          volume_name    = "${docker_volume.mysql_data_volume.name}"
          container_path = "/var/lib/mysql"
        }
        networks_advanced {
          name    = "${docker_network.private_bridge_network.name}"
          aliases = ["${var.mysql_network_alias}"]
        }
      }
      
      resource "null_resource" "sleep" {
        depends_on = ["docker_container.mysql_container"]
        provisioner "local-exec" {
          command = "sleep 15s"
        }
      }
      
      resource "docker_container" "blog_container" {
        name  = "ghost_blog"
        image = "${docker_image.ghost_image.name}"
        depends_on = ["null_resource.sleep", "docker_container.mysql_container"]
        env   = [
          "database__client=mysql",
          "database__connection__host=${var.mysql_network_alias}",
          "database__connection__user=${var.ghost_db_username}",
          "database__connection__password=${var.mysql_root_password}",
          "database__connection__database=${var.ghost_db_name}"
        ]
        ports {
          internal = "2368"
          external = "${var.ext_port}"
        }
        networks_advanced {
          name    = "${docker_network.public_bridge_network.name}"
          aliases = ["${var.ghost_network_alias}"]
        }
        networks_advanced {
          name    = "${docker_network.private_bridge_network.name}"
          aliases = ["${var.ghost_network_alias}"]
        }
      }
    4. Initialize Terraform:
      terraform init
    5. Validate the files:
      terraform validate
    6. Build a plan:
      terraform plan -out=tfplan -var 'ext_port=8082'
    7. Apply the plan:
      terraform apply tfplan
    8. List Docker volumes:
      docker volume inspect mysql_data
    9. List the data in mysql_data:
      sudo ls /var/lib/docker/volumes/mysql_data/_data
    10. Destroy the environment:
      terraform destroy -auto-approve -var 'ext_port=8082'
  13. Using Secrets
    1. Setup the environment:
      mkdir secrets
      cd secrets
      
    2. Encode the password with Base64:
      echo 'p4sSWoRd0!' | base64
      
    3. Create variables.tf:
      vi variables.tf
      

      variables.tf contents:

      variable "mysql_root_password" {
        default     = "cDRzU1dvUmQwIQo="
      }
      
      variable "mysql_db_password" {
        default     = "cDRzU1dvUmQwIQo="
      }
      
    4. Create image.tf:
      vi image.tf
      

      image.tf contents:

      resource "docker_image" "mysql_image" {
        name = "mysql:5.7"
      }
      
    5. Create secrets.tf:
      vi secrets.tf
      

      secrets.tf contents:

      resource "docker_secret" "mysql_root_password" {
        name = "root_password"
        data = "${var.mysql_root_password}"
      }
      
      resource "docker_secret" "mysql_db_password" {
        name = "db_password"
        data = "${var.mysql_db_password}"
      }
      
    6. Create networks.tf:
      vi networks.tf
      

      networks.tf contents:

      resource "docker_network" "private_overlay_network" {
        name     = "mysql_internal"
        driver   = "overlay"
        internal = true
      }
      
    7. Create volumes.tf:
      vi volumes.tf
      

      volumes.tf contents:

      resource "docker_volume" "mysql_data_volume" {
        name = "mysql_data"
      }
      
    8. Create main.tf:
      vi main.tf
      

      main.tf contents:

      resource "docker_service" "mysql-service" {
        name = "mysql_db"
      
        task_spec {
          container_spec {
            image = "${docker_image.mysql_image.name}"
      
            secrets = [
              {
                secret_id   = "${docker_secret.mysql_root_password.id}"
                secret_name = "${docker_secret.mysql_root_password.name}"
                file_name   = "/run/secrets/${docker_secret.mysql_root_password.name}"
              },
              {
                secret_id   = "${docker_secret.mysql_db_password.id}"
                secret_name = "${docker_secret.mysql_db_password.name}"
                file_name   = "/run/secrets/${docker_secret.mysql_db_password.name}"
              }
            ]
      
            env {
              MYSQL_ROOT_PASSWORD_FILE = "/run/secrets/${docker_secret.mysql_root_password.name}"
              MYSQL_DATABASE           = "mydb"
              MYSQL_PASSWORD_FILE      = "/run/secrets/${docker_secret.mysql_db_password.name}"
            }
      
            mounts = [
              {
                target = "/var/lib/mysql"
                source = "${docker_volume.mysql_data_volume.name}"
                type   = "volume"
              }
            ]
          }
          networks = [
            "${docker_network.private_overlay_network.name}"
          ]
        }
      }
      
    9. Initialize Terraform:
      terraform init
      
    10. Validate the files:
      terraform validate
      
    11. Build a plan:
      terraform plan -out=tfplan
      
    12. Apply the plan:
      terraform apply tfplan
      
    13. Find the MySQL container:
      docker container ls
      
    14. Use the exec command to log into the MySQL container:
      docker container exec -it [CONTAINER_ID] /bin/bash
      
    15. Access MySQL:
      mysql -u root -p
      
    16. Destroy the environment:
      terraform destroy -auto-approve
  14. Terraform Remote State
    1. Create an S3 Bucket

      1. Search for S3 in Find Services.

      2. Click Create Bucket.

      3. Enter a Bucket name. The bucket name must be unique.

      4. Make sure the Region is US East (N. Virginia) Click Next.

      5. Click Next again on the Configure options page.

      6. Click Next again on the Set permissions page.

      7. Click Create bucket on the Review page.

    2. Add the Terraform Folder to the Bucket

      1. Click on the bucket name.

      2. Click Create folder.

      3. Enter terraform-aws for the folder name.

      4. Click Save.

    3. Add Backend to Your Scripts

      1. From the Docker Swarm Manager navigate to the AWS directory:

        cd ~/terraform/AWS
        
      2. Set the Environment Variables

        export AWS_ACCESS_KEY_ID="[ACCESS_KEY]"
        export AWS_SECRET_ACCESS_KEY="[SECRET_KEY]]"
        export AWS_DEFAULT_REGION="us-east-1"
        
      3. Create terraform.tf:
        vi terraform.tf
        

        terraform.tf contents:

        terraform {
          backend "s3" {
            key    = "terraform-aws/terraform.tfstate"
          }
        }
        
      4. Initialize Terraform:
        terraform init -backend-config "bucket=[BUCKET_NAME]"
        
      5. Validate changes:
        terraform validate
        
      6. Plan the changes:
        terraform plan
        
      7. Apply the changes:
        terraform apply -auto-approve
        
      8. Destroy environment:
        terraform destroy -auto-approve
Leave A Reply

Your email address will not be published.

baseofporn.com https://www.opoptube.com
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.