# Utilize hashicorp/terraform image as CLI

**URL:** <https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794>\
**Category:** Terraform\
**Created:** [December 27, 2019, 10:50am UTC](https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794 "2019-12-27T10:50:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alchemist\_ubi](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alchemist_ubi/32/29321_2.png) [@alchemist\_ubi](https://discuss.hashicorp.com/u/alchemist_ubi)\
**Post date:** [December 27, 2019, 10:50am UTC](https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794/1 "2019-12-27T10:50:33Z")

</div>

Trying to use `hashicorp/terraform:light` as my CLI instead of installing everything locally on my computer using `docker-compose`.

The following configuration is what I had initially:

```auto
# docker-compose.yml
version: "3.7"

services:
  terraform:
    image: hashicorp/terraform:light

```

When I run the following command from the official documentation

```auto
docker-compose run terraform plan main.tf

```

I get

```auto
stat main.tf: no such file or directory

```

Since I didn’t copy my templates inside the image, the command is not able to find `main.tf` template.

I read [https://hub.docker.com/r/hashicorp/terraform](https://hub.docker.com/r/hashicorp/terraform)

And I don’t see any documentation about it, so I did the following.

```auto
# docker-compose.yml
version: "3.7"

services:
  terraform:
    image: hashicorp/terraform:light
    volumes:
    - ./terraform:/terraform

```

Notice that I mounted `./terraform` as `/terraform` inside the container.

Now you can the command again with the file path relative to `/terraform`.

```auto
docker-compose run terraform plan /terraform/main.tf

```

I am not sure if `/terraform` is a folder I should be using for this since there is no documentation about it, but it is working so far.

I hope you find this useful since I didn’t find any docs online at the moment,

The existing problem, using local module references as follow:

```auto
module "digitalocean" {
  source = "../digitalocean"
}

```

Causes the following issue:

```auto

This module is not yet installed. Run "terraform init" to install all modules
required by this configuration.

```

I am stuck at this step and would be great if I get any help on it.

Cheers.

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [January 2, 2020, 6:36pm UTC](https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794/2 "2020-01-02T18:36:18Z")

</div>

Hi @alchemist_ubi,

While it is technically possible to use the docker images in this way, it’s often quite complicated to do so because the natural isolation created by docker is kinda at odds with convenient CLI usage that tends to depend on access to local files in the host.

In your case it seems like the problem is that you’ve exposed the root module into the container but that module expects to find a sibling directory `digitalocean` which is _not_ exposed into the container. To make this work, you’ll need to arrange for your entire configuration tree to be available in the container, and then you can use the `working_dir` option to ensure that Terraform is running in whatever directory contains your _root_ module.

With all of that said, Terraform is distributed just as a single static executable, and so it’s often easier to just download and extract that executable onto your system than to fuss with docker. The docker images are there primarily to help with automation scenarios built around docker, rather than for interactive use. As a result, they will always require extra arguments to customize them for whatever automation environment they are running in.

The images themselves are literally just alpine linux images with the `terraform` executable added at `/bin/terraform`, a custom entrypoint referring to it, and `git` and `ssh` installed from the Alpine package manager. Any further customization and configuration is left to the user, because what is needed depends a lot on how the image is being integrated into a broader system.

---

<div class="post-metadata">

**Author:** ![alchemist\_ubi](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/alchemist_ubi/32/29321_2.png) [@alchemist\_ubi](https://discuss.hashicorp.com/u/alchemist_ubi)\
**Post date:** [January 3, 2020, 12:43am UTC](https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794/3 "2020-01-03T00:43:49Z")

</div>

Yeah, I gave up on the idea honestly, I wanted to keep adding AWS CLI and other tools.

I got it working, but then the SSH forwarding still an issue for Docker, so it was a pain downloading private modules.

Thanks a lot for the help

---

<div class="post-metadata">

**Author:** ![elreydetoda](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/elreydetoda/32/6118_2.png) [@elreydetoda](https://discuss.hashicorp.com/u/elreydetoda)\
**Post date:** [April 19, 2020, 11:13am UTC](https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794/4 "2020-04-19T11:13:19Z")

</div>

So, I just went through the process of figuring out how to do this (use the hashicorp/terraform image through the cli) and I did a short write up (very rough and really just a note to myself) on how to this and the exact command you will need are here: [https://blog.elreydetoda.site/docker-terraform/#generalcommands](https://blog.elreydetoda.site/docker-terraform/#generalcommands)  
the rest of the blog post tries to explain why you need to do those specific command, but comment either here or on the blog and let me know if you are confused about anything.

---

<div class="post-metadata">

**Author:** ![elreydetoda](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/elreydetoda/32/6118_2.png) [@elreydetoda](https://discuss.hashicorp.com/u/elreydetoda)\
**Post date:** [January 16, 2021, 2:11am UTC](https://discuss.hashicorp.com/t/utilize-hashicorp-terraform-image-as-cli/4794/5 "2021-01-16T02:11:18Z")

</div>

I changed the layout on my blog, so now the link is: [Terraforming with a whale (docker)](https://blog.elreydetoda.site/docker-terraform/#general-commands)
