Home DevOps Terraform Running Terraform in Docker Locally

Running Terraform in Docker Locally

Reading Time: 2 minutes

Running Terraform in Docker Locally

Here are some quick tips on how to run Terraform locally in Docker.

To get started pull the Docker image of Terraform. This image is built on the golang/alpine image.

Running a Script

There’s a couple of things here worth noting. I’m mapping in my scripts using the volume command in Docker. The entry point script executes Terraform so everything will run against Terraform.

On Linux use $(pwd) and in PowerShell use ${pwd}

Shout out to Victor Leong. I updated this post to use the “-w” flag and change the workspace after I realized the init command was not working as expected. I recommend reading his article. He goes into a lot of details that I left out.

Victor Legon – Terraform with Docker

I also recommend reading my friend Alex’s blog on Terraform. He has some awesome suggestions on things he learned while picking it up.

Copying SSH Keys to Docker Container

You may need to use SSH Keys and here’s an easy way to copy them.

Start by copying your local ssh keys into a directory you will map into the container.

Once in the container here’s what you will need to do to set them up under the root user.

Note: If you experience issues with files ending with a “?” (id_rsa?) it’s because your script isn’t running with line endings of LF. This happens because of Window’s line endings are CRLF.

Running Terraform in Detached State

If you want shell access to the Terraform container here’s how. Docker containers are designed to shut down immediately after running, if the entrypoint command is complete. To keep the container running use the command “sh tail -f /dev/null”.

Useful Terraform Commands

Terraform init

Will initialize a working directory to install plugins and configuration files.

Terraform validate

Will validate your Terraform script for any syntax errors.

Terraform Turn on Error Logging

Choose one of the export TF_LOG options below to set your logging level.