Skip to content

Instantly share code, notes, and snippets.

@san-tak
Last active April 16, 2019 11:29
Show Gist options
  • Save san-tak/a4ea1f94905fae7ce1025b1bf5ef76f2 to your computer and use it in GitHub Desktop.
Save san-tak/a4ea1f94905fae7ce1025b1bf5ef76f2 to your computer and use it in GitHub Desktop.
Terraform を Docker Desktop for Mac で動かす

概要

Docker Desktop for Mac上で Terraform コンテナを動かして、Docker Desktop for Mac上のコンテナ構築を行う、手順を説明します。イメージは以下の通りです。

  • Docker Desktop for Mac 上で、と書いてありますが特にfor Mac 固有の事項はありません。

terraform_struct.png

準備

1. Terraform のコンテナを取得

$ docker pull hashicorp/terraform:light
$ docker run --rm -it hashicorp/terraform:light version 
Terraform v0.11.13

2. 作業用のフォルダを作成

任意の作業用フォルダを作成します。ここに、Terraform の各種情報が格納されます。

$ mkdir ~/tf_test

tf ファイルの作成

以下、nginx の構築用tf ファイルを作業用のフォルダに作成します。

$ cd ~/tf_test
$ vi main.tf

main.tf に書く内容例:

provider "docker" {
  host = "unix:///var/run/docker.sock"
}

# Create a container
resource "docker_container" "nginx" {
  image = "${docker_image.nginx.latest}"
  name  = "nginx"
  ports {
    internal = 80
    external = 8080
  }
}

resource "docker_image" "nginx" {
  name = "nginx:latest"
}

Terraform (Dockerコンテナ)からコンテナを作成する

初期化(init)

この操作で、カレントディレクトリに.terraform が出来て、その中にdocker プロバイダが作成されます。

$ docker run --rm -it -v $PWD:/app/ -w /app/ hashicorp/terraform:light init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "docker" (1.1.1)...

...

Terraform has been successfully initialized!
...

Dry run (plan)

$ docker run --rm -it -v $PWD:/app/ -v /var/run/docker.sock:/var/run/docker.sock -w /app/ hashicorp/terraform:light plan
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.nginx
      id:               <computed>
      attach:           "false"
      bridge:           <computed>
      container_logs:   <computed>
      exit_code:        <computed>
      gateway:          <computed>
      image:            "${docker_image.nginx.latest}"
      ip_address:       <computed>
      ip_prefix_length: <computed>
      log_driver:       "json-file"
      logs:             "false"
      must_run:         "true"
      name:             "nginx"
      network_data.#:   <computed>
      ports.#:          "1"
      ports.0.external: "8080"
      ports.0.internal: "80"
      ports.0.ip:       "0.0.0.0"
      ports.0.protocol: "tcp"
      restart:          "no"
      rm:               "false"
      start:            "true"

  + docker_image.nginx
      id:               <computed>
      latest:           <computed>
      name:             "nginx:latest"


Plan: 2 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.

実際の配備 (apply)

$ docker run --rm -it -v $PWD:/app/ -v /var/run/docker.sock:/var/run/docker.sock -w /app/ hashicorp/terraform:light apply

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.nginx
      id:               <computed>
      attach:           "false"
      bridge:           <computed>
      container_logs:   <computed>
      exit_code:        <computed>
      gateway:          <computed>
      image:            "${docker_image.nginx.latest}"
      ip_address:       <computed>
      ip_prefix_length: <computed>
      log_driver:       "json-file"
      logs:             "false"
      must_run:         "true"
      name:             "nginx"
      network_data.#:   <computed>
      ports.#:          "1"
      ports.0.external: "8080"
      ports.0.internal: "80"
      ports.0.ip:       "0.0.0.0"
      ports.0.protocol: "tcp"
      restart:          "no"
      rm:               "false"
      start:            "true"

  + docker_image.nginx
      id:               <computed>
      latest:           <computed>
      name:             "nginx:latest"


Plan: 2 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_image.nginx: Creating...
  latest: "" => "<computed>"
  name:   "" => "nginx:latest"
docker_image.nginx: Still creating... (10s elapsed)
docker_image.nginx: Still creating... (20s elapsed)
docker_image.nginx: Creation complete after 27s (ID: sha256:881bd08c0b08234bd19136957f15e430...646c1e700f7fea26e41fc40069nginx:latest)
docker_container.nginx: Creating...
  attach:           "" => "false"
  bridge:           "" => "<computed>"
  container_logs:   "" => "<computed>"
  exit_code:        "" => "<computed>"
  gateway:          "" => "<computed>"
  image:            "" => "sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069"
  ip_address:       "" => "<computed>"
  ip_prefix_length: "" => "<computed>"
  log_driver:       "" => "json-file"
  logs:             "" => "false"
  must_run:         "" => "true"
  name:             "" => "nginx"
  network_data.#:   "" => "<computed>"
  ports.#:          "" => "1"
  ports.0.external: "" => "8080"
  ports.0.internal: "" => "80"
  ports.0.ip:       "" => "0.0.0.0"
  ports.0.protocol: "" => "tcp"
  restart:          "" => "no"
  rm:               "" => "false"
  start:            "" => "true"
docker_container.nginx: Creation complete after 0s (ID: 7b3068a33750735565ce83914d9a623d70aa9f41d2136b9bcd8b287fa49d3014)

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

確認

$ docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                      PORTS                                                   NAMES
3ddd1b137c61        881bd08c0b08              "nginx -g 'daemon of…"   4 seconds ago       Up 2 seconds                80/tcp                                                  nginx
$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
$ docker run --rm -it -v $PWD:/app/ -v /var/run/docker.sock:/var/run/docker.sock -w /app/ hashicorp/terraform:light show
docker_container.nginx:
  id = 7b3068a33750735565ce83914d9a623d70aa9f41d2136b9bcd8b287fa49d3014
  attach = false
  bridge = 
  gateway = 172.17.0.1
  image = sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069
  ip_address = 172.17.0.3
  ip_prefix_length = 16
  log_driver = json-file
  logs = false
  must_run = true
  name = nginx
  network_data.# = 1
  network_data.0.gateway = 172.17.0.1
  network_data.0.ip_address = 172.17.0.3
  network_data.0.ip_prefix_length = 16
  network_data.0.network_name = bridge
  ports.# = 1
  ports.0.external = 8080
  ports.0.internal = 80
  ports.0.ip = 0.0.0.0
  ports.0.protocol = tcp
  restart = no
  rm = false
  start = true
docker_image.nginx:
  id = sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069nginx:latest
  latest = sha256:881bd08c0b08234bd19136957f15e4301097f4646c1e700f7fea26e41fc40069
  name = nginx:latest

削除 (destroy)

$ docker run --rm -it -v $PWD:/app/ -v /var/run/docker.sock:/var/run/docker.sock -w /app/ hashicorp/terraform:light destroy
docker_image.nginx: Refreshing state... (ID: sha256:881bd08c0b08234bd19136957f15e430...646c1e700f7fea26e41fc40069nginx:latest)
docker_container.nginx: Refreshing state... (ID: 7b3068a33750735565ce83914d9a623d70aa9f41d2136b9bcd8b287fa49d3014)

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

Terraform will perform the following actions:

  - docker_container.nginx

  - docker_image.nginx


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

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_container.nginx: Destroying... (ID: 7b3068a33750735565ce83914d9a623d70aa9f41d2136b9bcd8b287fa49d3014)
docker_container.nginx: Destruction complete after 0s
docker_image.nginx: Destroying... (ID: sha256:881bd08c0b08234bd19136957f15e430...646c1e700f7fea26e41fc40069nginx:latest)
docker_image.nginx: Destruction complete after 1s

Destroy complete! Resources: 2 destroyed.
Mac-mini-2018:tf01 takao$ docker run --rm -it hashicorp/terraform:light version 
Terraform v0.11.13

参考資料

Qiita

その他

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment