Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The Simplest Terraform with Docker on macOS

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:

1. Install Docker

Install Docker for Mac if you have not already.

2. Install Terraform

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

3. Configure, Initialize, Plan & Apply!

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

Initialize Terraform:

terraform init

Generate Plan

Now, if the output from terraform init was without error, generate the plan:

terraform plan

Apply 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 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.

@mrvindu
Copy link

mrvindu commented Nov 21, 2019

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

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