Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kamal2222ahmed/cdde1678b7aacc42b6e7d14f72249c23 to your computer and use it in GitHub Desktop.
Save kamal2222ahmed/cdde1678b7aacc42b6e7d14f72249c23 to your computer and use it in GitHub Desktop.
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 for macOS from releases.hashicorp.com and place the terafform binary somewhere in your PATH or you can install with Homebrew:

brew install terraform

3. Configure, Plan & Apply!

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!

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