Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(1)Terraform Basics and a Docker Deployment-(10)Modules - The Container Module

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
1. Terraform Basics and a Docker Deployment
10. Modules - The Container Module

In the last post, we have setup the "image" module. In this post, we will setup the "container" module.

~/docker/image# ls

main.tf  outputs.tf  terraform.tfstate  terraform.tfstate.backup  variables.tf

~/docker/image# cd ../container

~/docker/container# vim main.tf
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
    docker = {
      source = "terraform-providers/docker"
    }
  }
  required_version = ">= 0.13"
}

# Start the Container
resource "docker_container" "container_id" {
  name  = var.name
  image = var.image
  ports {
    internal = var.int_port
    external = var.ext_port
  }
}

~/docker/container# vim variables.tf
variable "image" {}
variable "name" {}
variable "int_port" {}
variable "ext_port" {}

~/docker/container# vim outputs.tf
# Output the IP Address and name of the container
output "ip" {
  value = docker_container.container_id.ip_address
}

output "container_name" {
  value = docker_container.container_id.name
}

~/docker/container# terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of terraform-providers/docker...
- Finding latest version of hashicorp/aws...
- Installing terraform-providers/docker v2.7.2...
- Installed terraform-providers/docker v2.7.2 (signed by HashiCorp)
- Installing hashicorp/aws v3.9.0...
- Installed hashicorp/aws v3.9.0 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/aws: version = "~> 3.9.0"
* terraform-providers/docker: version = "~> 2.7.2"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

~/docker/container# terraform plan
var.ext_port
  Enter a value: 80

var.image
  Enter a value: ghost:alpine

var.int_port
  Enter a value: 2368

var.name
  Enter a value: blog

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.container_id will be created
  + resource "docker_container" "container_id" {
      + attach           = false
      + bridge           = (known after apply)
      + command          = (known after apply)
      + container_logs   = (known after apply)
      + dns              = (known after apply)
      + dns_opts         = (known after apply)
      + entrypoint       = (known after apply)
      + exit_code        = (known after apply)
      + gateway          = (known after apply)
      + hostname         = (known after apply)
      + id               = (known after apply)
      + image            = "ghost:alpine"
      + ip_address       = (known after apply)
      + ip_prefix_length = (known after apply)
      + ipc_mode         = (known after apply)
      + log_driver       = (known after apply)
      + log_opts         = (known after apply)
      + logs             = false
      + must_run         = true
      + name             = "blog"
      + network_data     = (known after apply)
      + read_only        = false
      + restart          = "no"
      + rm               = false
      + shm_size         = (known after apply)
      + start            = true
      + user             = (known after apply)
      + working_dir      = (known after apply)

      + ports {
          + external = 80
          + internal = 2368
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

~/docker/container# terraform apply
var.ext_port
  Enter a value: 80

var.image
  Enter a value: ghost:alpine

var.int_port
  Enter a value: 2368

var.name
  Enter a value: blog


An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.container_id will be created
  + resource "docker_container" "container_id" {
      + attach           = false
      + bridge           = (known after apply)
      + command          = (known after apply)
      + container_logs   = (known after apply)
      + dns              = (known after apply)
      + dns_opts         = (known after apply)
      + entrypoint       = (known after apply)
      + exit_code        = (known after apply)
      + gateway          = (known after apply)
      + hostname         = (known after apply)
      + id               = (known after apply)
      + image            = "ghost:alpine"
      + ip_address       = (known after apply)
      + ip_prefix_length = (known after apply)
      + ipc_mode         = (known after apply)
      + log_driver       = (known after apply)
      + log_opts         = (known after apply)
      + logs             = false
      + must_run         = true
      + name             = "blog"
      + network_data     = (known after apply)
      + read_only        = false
      + restart          = "no"
      + rm               = false
      + shm_size         = (known after apply)
      + start            = true
      + user             = (known after apply)
      + working_dir      = (known after apply)

      + ports {
          + external = 80
          + internal = 2368
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

docker_container.container_id: Creating...
docker_container.container_id: Still creating... [10s elapsed]
docker_container.container_id: Still creating... [20s elapsed]
docker_container.container_id: Creation complete after 29s [id=7cfff19f1d5530388e93b3232e3599ed253623ef38898f87a6e9d8a2bde5eb7a]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

container_name = blog
ip = 172.17.0.2



Category: orchestration Tags: public

Upvote


Downvote