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 binary for Darwin/macOS from
releases.hashicorp.com
and place the terraform
binary somewhere in your PATH
.
You can install also install Terraform with Homebrew like this:
brew install terraform
Start with a basic NGINX Docker container definition in a minimal Terraform
configuration — create a main.tf
file with this command:
cat > main.tf << EOF
# Configure Docker provider and connect to the local Docker socket
provider "docker" {
host = "unix:///var/run/docker.sock"
}
# Create 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"
}
EOF
terraform init
Now, if the output from terraform init
was without error, generate the plan:
terraform plan
Finally, if the plan is all good and without error, apply it:
terraform apply -auto-approve
The apply should complete without error and if so, you can move on to verifying the container status.
Verify that the container is running:
docker ps -f name=enginecks --format "table {{.Names}}\t{{.Status}}"
The output should have something like this:
NAMES STATUS
enginecks Up 17 seconds
Looks like the NGINX container (named enginecks) is up!
Now visit http://localhost in your browser and you should observe the default Welcome to nginx! default page.
Hello Dear,
Din't find this stuff internet anywhere.
I would like do swarm cluster like this, not able to to work around.
Could you help me with this /
Regards
Aravind