If you'd like to experiment with Terraform on macOS locally, a great provider for doing so is the Docker provider. You can get set up in a few simple steps, like so:
Install Docker for Mac if you have not already.
Grab the latest Terraform for macOS from
releases.hashicorp.com
and place the terafform
binary somewhere in your PATH
or you can install
with Homebrew:
brew install terraform
Start with a basic NGINX Docker container definition in a minimal Terraform
configuration — create a main.tf
file, and add this to it:
# Configure Docker provider and connect to the local Docker socket
provider "docker" {
host = "unix:///var/run/docker.sock"
}
# Create an Nginx container
resource "docker_container" "nginx" {
image = "${docker_image.nginx.latest}"
name = "enginecks"
ports {
internal = 80
external = 80
}
}
resource "docker_image" "nginx" {
name = "nginx:latest"
}
Save the file, then apply the configuration:
terraform plan
If the plan is good and without error, apply it:
terraform apply
Check to see that the container is running:
docker ps -a
The output should have something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41911ec0df6f 6b914bbcb89e "nginx -g 'daemon ..." 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, 443/tcp enginecks
Now visit http://localhost in your browser and you should see the Welcome to nginx! default page!