Join us

Managing Docker containers using Terraform on Windows 10

Terraform & Docker on W10

Today we will be looking at how to manage Docker containers on W10 using Terraform the IAC tool.

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It can manage existing and popular service providers as well as custom in-house solutions. It is a infrastructure as code, execution plan, resource graph, and change automation tool. It stops where Configuration Management tools like Puppet, Ansible and Chef starts.

One of the supported providers by Terraform is Docker. And, a priori, it looked simple enough to try and make a few terraform learnings. It is quick and local to my Windows 10 setup. So, pressing Windows key -> Docker Quickstart Terminal launches the Docker default VM (VirtualBox provider) via docker-machine.

The next step is to create a project, and to attempt to launch a simple Ubuntu container as per the tutorial (see https://www.terraform.io/docs/providers/docker/index.html).

In this example tutorial, the terraform source looks like this:

The above needs to be saved in a “*.tf” file, e.g. example.tf in a local directory. Note that “.tf” is the terraform file extension and all files found within a directory with this extension are automatically included when running a terraform command.

There I faced a first problem, which IP and protocol to use in the provider section? I knew that the IP is the Docker VirtualBox VM (192.168.99.100) but not the port, and the protocol should be http as the Remote Docker API is REST based.

Finding this information is easy. In the Quick Start console, simply type docker-machine config.

Thus the host IP/protocol is tcp://192.168.99.100:2376.

But after updating the example.tf file with that information, and trying a terraform plan command, I still got a malformed HTTP response error (second problem).

A quick Google search indicates no matching results for my Docker setup (Docker Toolbox, recent docker version). There are a few similar issues (here) but those are using boot2docker which has been deprecated in favor of docker-machine many releases ago. Also, a quick docker info will show that I am running release 1.12.1.

A netstat on the VM shows that port 2376 is open and associated with the docker server:

And a curl to the remote Docker API returns no visible results:

But works.

It is at this point that I make the connection with the certificates directory listed by the docker-machine config command, and the Docker Remote API documentation. Looking back to the terraform Docker documentation, it says:

The following arguments are supported:

While cert_path is indicated as optional, it seems that terraform might require this attribute to be set for on most deployments since docker-machine, as per Docker Remote API documentation, instantiates a Docker daemon that uses an encrypted TCP socket using TLS.

The working myexample.tf then becomes:

HashiCorp allows users to update the documentation via GitHub, which leads me to create a pull request to update it with at least the default dockerd port information.

And terraform plan now works:

Hope this helps you using docker on you Windows to automate your processes with Terraform.

#YouAreAwesome #StayAwesome


Pawfives by

Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

Apoti Eri

CEO, apotitech

@apotitech
A lover of Jesus and IT. I have a passion for teaching and sharing my knowledge with others. My very best time of the day is teaching and helping my peers.
User Popularity
96

Influence

10k

Total Hits

2

Posts