# Install terraform in docker container

On Debian

Ensure that your system is up to date and you have installed the `gnupg`, `software-properties-common`, and `curl` packages installed. You will use these packages to verify HashiCorp's GPG signature and install HashiCorp's Debian package repository.

```
$ sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
```

Install the HashiCorp [GPG key](https://apt.releases.hashicorp.com/gpg "HashiCorp GPG key").

```
$ wget -O- https://apt.releases.hashicorp.com/gpg | \gpg --dearmor | \sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
```

Verify the key's fingerprint.

```
$ gpg --no-default-keyring \--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \--fingerprint
```

The `gpg` command will report the key fingerprint:

```
/usr/share/keyrings/hashicorp-archive-keyring.gpg
-------------------------------------------------
pub   rsa4096 2023-01-10 [SC] [expires: 2028-01-09]
      798A EC65 4E5C 1542 8C8E  42EE AA16 FCBC A621 E701
uid           [ unknown] HashiCorp Security (HashiCorp Package Signing) <security+packaging@hashicorp.com>
sub   rsa4096 2023-01-10 [S] [expires: 2028-01-09]
```

<div class="mdx-inline-alert_spacing___QlMF" id="bkmrk--1"><div class="inline-alert_default___c6Mu hds-typography-body-200 mdx-inline-alert_typographyOverride__DHnxV"><span class="inline-alert_icon__b_Ema" data-testid="icon"><svg aria-hidden="true" fill="none" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g fill="currentColor"></g></svg></span></div></div><p class="callout info">Refer to the [Official Packaging Guide](https://www.hashicorp.com/official-packaging-guide) for the latest public signing key. You can also verify the key on [Security at HashiCorp](https://www.hashicorp.com/security) under **Linux Package Checksum Verification**.</p>

Add the official HashiCorp repository to your system. The `lsb_release -cs` command finds the distribution release codename for your current system, such as `buster`, `groovy`, or `sid`.

```
$ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \sudo tee /etc/apt/sources.list.d/hashicorp.list
```

Download the package information from HashiCorp.

```
$ sudo apt update
```

Install Terraform from the new repository.

```
$ sudo apt-get install terraform
```

If you use either Bash or Zsh, you can enable tab completion for Terraform commands. To enable autocomplete, first ensure that a config file exists for your chosen shell.

<div class="mdx-tabs_tabsWrapper__eBd6p" id="bkmrk--2"><div><div class="tabs_tabControls__T_UOv tabs_showAnchorLine__oSYFX tabs_allowNestedStyles__gpKFS"><div class="tab-button-controls_tabList__ueYEe" role="tablist">  
</div></div></div></div>```
$ touch ~/.bashrc
```

<div class="mdx-tabs_tabsWrapper__eBd6p" id="bkmrk--3"><div>  
</div></div>Then install the autocomplete package.

```
$ terraform -install-autocomplete
```

<p class="callout success">Once the autocomplete support is installed, you will need to restart your shell.</p>

Now that you've installed Terraform, you can provision an NGINX server in less than a minute using [Docker](https://www.docker.com/products/docker-desktop) on Mac, Windows, or Linux. You can also follow the rest of this tutorial in your web browser.

Click on the tab(s) below relevant to your operating system.

<div class="mdx-tabs_tabsWrapper__eBd6p" id="bkmrk--4"><div><div class="tabs_tabControls__T_UOv tabs_showAnchorLine__oSYFX tabs_allowNestedStyles__gpKFS"><div class="tab-button-controls_tabList__ueYEe" role="tablist">  
</div></div></div></div>To follow this tutorial on Linux, first [install Docker Engine](https://docs.docker.com/engine/install/) for your distribution.

<div class="mdx-tabs_tabsWrapper__eBd6p" id="bkmrk--5"><div><div aria-hidden="false" aria-labelledby="tab-2-react-aria-245" class="tabs_tabPanel__lKCo_ tabs_allowNestedStyles__gpKFS" id="bkmrk--6" role="tabpanel"></div></div></div>Create a directory named `learn-terraform-docker-container`.

```
$ mkdir learn-terraform-docker-container
```

This working directory houses the configuration files that you write to describe the infrastructure you want Terraform to create and manage. When you initialize and apply the configuration here, Terraform uses this directory to store required plugins, modules (pre-written configurations), and information about the real infrastructure it created.

Navigate into the working directory.

```
$ cd learn-terraform-docker-container
```

In the working directory, create a file called `main.tf` and paste the following Terraform configuration into it.

<div class="mdx-tabs_tabsWrapper__eBd6p" id="bkmrk--7"><div><div class="tabs_tabControls__T_UOv tabs_showAnchorLine__oSYFX tabs_allowNestedStyles__gpKFS"><div class="tab-button-controls_tabList__ueYEe" role="tablist">  
</div></div></div></div>```
terraform {  required_providers {    docker = {      source  = "kreuzwerker/docker"      version = "~> 3.0.1"    }  }}provider "docker" {}resource "docker_image" "nginx" {  name         = "nginx"  keep_locally = false}resource "docker_container" "nginx" {  image = docker_image.nginx.image_id  name  = "tutorial"  ports {    internal = 80    external = 8000  }}
```

<div class="mdx-tabs_tabsWrapper__eBd6p" id="bkmrk--8"><div>  
</div></div>Initialize the project, which downloads a plugin called a provider that lets Terraform interact with Docker.

```
$ terraform init
```

<details id="bkmrk-terraform-init-root%40"><summary>terraform init</summary>

root@dockerpx:/home/dockers/terraform# terraform init

Initializing the backend...

Initializing provider plugins...  
\- Finding kreuzwerker/docker versions matching "~&gt; 3.0.1"...  
\- Installing kreuzwerker/docker v3.0.2...  
\- Installed kreuzwerker/docker v3.0.2 (self-signed, key ID BD080C4571C6104C)

Partner and community providers are signed by their developers.  
If you'd like to know more about provider signing, you can read about it here:  
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has created a lock file .terraform.lock.hcl to record the provider  
selections it made above. Include this file in your version control repository  
so that Terraform can guarantee to make the same selections by default when  
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see  
any changes that are required for your infrastructure. All Terraform commands  
should now work.

If you ever set or change modules or backend configuration for Terraform,  
rerun this command to reinitialize your working directory. If you forget, other  
commands will detect it and remind you to do so if necessary.

</details>Provision the NGINX server container with `apply`. When Terraform asks you to confirm type `yes` and press `ENTER`.

```
$ terraform apply
```

Verify the existence of the NGINX container by visiting [localhost:8000](http://localhost:8000) in your web browser or running `docker ps` to see the container.

<span class="image_root__Kkwm7">![NGINX running in Docker via Terraform](https://developer.hashicorp.com/_next/image?url=https%3A%2F%2Fcontent.hashicorp.com%2Fapi%2Fassets%3Fproduct%3Dtutorials%26version%3Dmain%26asset%3Dpublic%252Fimg%252Fterraform%252Fgetting-started%252Fterraform-docker-nginx.png%26width%3D2048%26height%3D510&w=3840&q=75)</span>

```
$ docker psCONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES425d5ee58619        e791337790a6              "nginx -g 'daemon of…"   20 seconds ago      Up 19 seconds       0.0.0.0:8000->80/tcp     tutorial
```

To stop the container, run `terraform destroy`.

```
$ terraform destroy
```

You've now provisioned and destroyed an NGINX webserver with Terraform.