https://github.com/p4irin/explore_terraform_docker_nginx
Provision a Docker container running a default setup of nginx with Terraform
https://github.com/p4irin/explore_terraform_docker_nginx
docker explore nginx terraform
Last synced: 7 months ago
JSON representation
Provision a Docker container running a default setup of nginx with Terraform
- Host: GitHub
- URL: https://github.com/p4irin/explore_terraform_docker_nginx
- Owner: p4irin
- Created: 2025-01-21T18:22:18.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-02-12T18:15:22.000Z (8 months ago)
- Last Synced: 2025-02-12T19:29:16.173Z (8 months ago)
- Topics: docker, explore, nginx, terraform
- Language: HCL
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Explore Terraform
Provision and manage a Docker container running a default nginx setup
## Provision resources declared in `main.tf`
Initialize the project: downloads plugin/provider to interact with Docker
```bash
$ terraform init
```Provision nginx container
```bash
$ terraform apply
```Verify the image was downloaded
```bash
$ docker image ls|grep nginx
```Verify the container is running with
```bash
$ docker ps|grep nginx
```
or by visiting http://localhost:8000 in your browserShow the state of your infrastructure
```bash
$ terraform show
```Show a list of managed resources
```bash
$ terraform state list
```## Change the docker_container resource
Change the external port of the container to 8080
```hcl
resource "docker_container" "nginx" {
image = docker_image.nginx.image_id
name = "nginx"ports {
internal = 80
external = 8080
}
```Apply the resource change. The port of a running container can't be changed. Terraform will destroy the existing container and replace it with a new one.
```bash
$ terraform apply
```Verify the new container's external port is 8080
```bash
$ docker ps
```
or visit http://localhost:8080.
or```bash
$ terraform state show docker_container.nginx
```## Use a variable for the container name
Refer to the content of `variables.tf` which declares the `container_name` variable used in `main.tf`.
With these files and variable declaration in place, applying the configuration
```bash
$ terraform apply
...
```wil result in a container with the default name `ExampleNginxContainer`. Verify with
```bash
$ docker ps
```When applying the config. you can pass a name of your choosing with the `-var` option:
```bash
$ terraform apply -var "container_name=ADefaultNginxSetup"
```
### Reference* [Customize Terraform configuration with variables](https://developer.hashicorp.com/terraform/tutorials/configuration-language/variables)
### Labs
* [lab](https://kodekloud.com/pages/free-labs/terraform/terraform-variables)
* [lab](https://kodekloud.com/pages/free-labs/terraform/terraform-variables-2)## Query and display data
Refer to the file `outputs.tf`. It declares _outputs_ that query/represents the nginx _container id_ and _image id_.
Assuming you already applied your config. including this file you can use/query the output values:
```bash
$ terraform output
container_id = "c0c0742..."
image_id = "sha256:9bea9f..."
```### Reference
* [Output data from Terraform](https://developer.hashicorp.com/terraform/tutorials/configuration-language/outputs)
### Labs
* [lab](https://kodekloud.com/pages/free-labs/terraform/terraform-output-variables)
## Destroy provisioned resources
```bash
$ terraform destroy
```Verify destruction of image and container
```bash
$ docker image ls|grep nginx
$ docker ps|grep nginx
$ terraform show
```## Reference
* [Docker provider documentation](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs)
* [Terraform registry](https://registry.terraform.io/)
* [Terraform](https://developer.hashicorp.com/terraform)