This image extends the hashicorp/terraform image so that it can write state to docker volumes.
255
A number of use cases lend themselves well to running terraform inside a docker container. These include
Our example will use a simple terraform module in github that creates VPCs.
First we create a docker volume (called vol.tfstate) and add the terraform module code to it by way of an alpine git container.
docker volume create vol.tfstate
docker run --interactive \
--tty \
--rm \
--volume vol.tfstate:/terraform-work \
alpine/git \
clone https://github.com/devops4me/terraform-aws-vpc-network /terraform-work
sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data
When you list the files in the container you will see the terraform module's contents there.
As our volume contains the terraform module code from git we are now ready to perform a terraform init. We use the devops4me/terraform container container which adds a VOLUME mapping to the hashicorp/terraform container at the /terraform-work location.
example - there is a working example directory in the git terraform module that demonstrates module use and is used by continuous integration actors.
docker run --interactive \
--tty \
--rm \
--name vm.terraform \
--volume vol.tfstate:/terraform-work \
devops4me/terraform init example
sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data
The directory listing verifies that our volume now contains a .terraform directory.
At last we can run the terraform apply. Provide a role arn only if your organization works with roles alongside the other 3 AWS authentication keys.
docker run --interactive \
--tty \
--rm \
--name vm.terraform \
--env AWS_DEFAULT_REGION=<<aws-region-key>> \
--env AWS_ACCESS_KEY_ID=<<aws-access-key>> \
--env AWS_SECRET_ACCESS_KEY=<<aws-secret-key>> \
--env TF_VAR_in_role_arn=<<aws-role-arn>> \
--volume vol.tfstate:/terraform-work \
devops4me/terraform apply -auto-approve example
sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data
Examining the docker volume should reveal a tfstate file which documents the state of your infrastructure just after the terraform apply execution.
The benefits of running terraform in a docker container with docker volumes really start to reveal themselves at this stage.You can create another infrastructure simply by using another docker volume. Your host environment need not even have terraform installed. You can git clone different branches and commits. You can use different terraform versions to build the infrastructure.
After running plan and apply either once or multiple times you may feel the need to terraform destroy the infrastructure.
docker run --interactive \
--tty \
--rm \
--name vm.terraform \
--env AWS_DEFAULT_REGION=<<aws-region-key>> \
--env AWS_ACCESS_KEY_ID=<<aws-access-key>> \
--env AWS_SECRET_ACCESS_KEY=<<aws-secret-key>> \
--env TF_VAR_in_role_arn=<<aws-role-arn>> \
--volume vol.tfstate:/terraform-work \
devops4me/terraform destroy -auto-approve example
sudo ls -lah /var/lib/docker/volumes/vol.tfstate/_data
Verify the destroy from your AWS console and also check that your volume now has a tfstate backup file created by terraform.
Content type
Image
Digest
Size
43.7 MB
Last updated
over 5 years ago
docker pull devops4me/terraform