11import os
22project_name = "app/media/MyTerraform"
33modules_dir = os .path .join (project_name , "modules" )
4- docker_dir = os .path .join (modules_dir , "docker " )
4+ docker_container_dir = os .path .join (modules_dir , "docker_container " )
55
66# Create project directories
7- os .makedirs (docker_dir , exist_ok = True )
7+ os .makedirs (docker_container_dir , exist_ok = True )
88
9- # Create main.tf at root
9+ # Create main.tf
1010with open (os .path .join (project_name , "main.tf" ), "w" ) as main_file :
11- main_file .write ('''provider "docker" {
11+ main_file .write ('''
12+ provider "docker" {
1213 host = var.docker_host
1314}
1415
15- module "docker" {
16- source = "./modules/docker"
17- image = var.image
18- name = var.container_name
19- ports = var.ports
16+ module "docker_container" {
17+ source = "./modules/docker_container"
18+
19+ image = var.image
20+ name = var.name
21+ ports = var.ports
22+ env = var.env
2023}
24+
2125''' )
2226
23- # Create variables.tf at root
24- with open (os .path .join (project_name , "variables.tf" ), "w" ) as variables_file :
25- variables_file .write ('''variable "docker_host" {
26- description = "The Docker host Uri."
27+ # Create variables.tf
28+ with open (os .path .join (project_name , "variables.tf" ), "w" ) as vars_file :
29+ vars_file .write ('''
30+ variable "docker_host" {
31+ description = "Docker host URL."
2732 type = string
2833}
2934
3035variable "image" {
31- description = "The Docker image to use."
36+ description = "Docker image to use."
3237 type = string
3338}
3439
35- variable "container_name " {
36- description = "The name of the Docker container."
40+ variable "name " {
41+ description = "Name of the container."
3742 type = string
3843}
3944
4045variable "ports" {
4146 description = "List of ports to expose."
4247 type = list(string)
4348}
49+
50+ variable "env" {
51+ description = "Environment variables for the container."
52+ type = map(string)
53+ }
4454''' )
4555
46- # Create terraform.tfvars at root
56+ # Create terraform.tfvars
4757with open (os .path .join (project_name , "terraform.tfvars" ), "w" ) as tfvars_file :
48- tfvars_file .write ('''docker_host = "tcp://localhost:2375"
49- image = "nginx:latest"
50- container_name = "my_nginx"
58+ tfvars_file .write ('''
59+ docker_host = "tcp://localhost:2375"
60+ image = "nginx:latest"
61+ name = "my-nginx-container"
5162ports = ["80:80"]
63+ env = { "MY_ENV_VAR" = "value" }
5264''' )
5365
54- # Create versions.tf at root
66+ # Create versions.tf
5567with open (os .path .join (project_name , "versions.tf" ), "w" ) as versions_file :
56- versions_file .write ('''terraform {
68+ versions_file .write ('''
69+ terraform {
5770 required_version = ">= 1.0"
5871
5972 required_providers {
6073 docker = {
61- source = "hashicorp /docker"
62- version = ">= 2.0"
74+ source = "kreuzwerker /docker"
75+ version = ">= 2.8. 0"
6376 }
6477 }
6578}
79+
6680''' )
6781
68- # Create outputs.tf at root
82+ # Create outputs.tf
6983with open (os .path .join (project_name , "outputs.tf" ), "w" ) as outputs_file :
70- outputs_file .write ('''output "container_id" {
84+ outputs_file .write ('''
85+ output "container_id" {
7186 description = "The ID of the Docker container."
72- value = module.docker .container_id
87+ value = module.docker_container .container_id
7388}
7489
7590output "container_ip" {
7691 description = "The IP address of the Docker container."
77- value = module.docker .container_ip
92+ value = module.docker_container .container_ip
7893}
7994''' )
8095
81- # Create main.tf in modules/docker
82- with open (os .path .join (docker_dir , "main.tf" ), "w" ) as docker_main_file :
83- docker_main_file .write ('''resource "docker_container" "this" {
84- name = var.name
96+ # Create module files
97+ # Create docker_container/main.tf
98+ with open (os .path .join (docker_container_dir , "main.tf" ), "w" ) as module_main_file :
99+ module_main_file .write ('''
100+ resource "docker_container" "app" {
85101 image = var.image
102+ name = var.name
86103 ports {
87- internal = var.ports[0]
88- external = var.ports[1 ]
104+ internal = 80
105+ external = var.ports[0 ]
89106 }
107+ env = var.env
90108}
91109''' )
92110
93- # Create variables.tf in modules/docker
94- with open (os .path .join (docker_dir , "variables.tf" ), "w" ) as docker_variables_file :
95- docker_variables_file .write ('''variable "image" {
96- description = "The Docker image to use."
111+ # Create docker_container/variables.tf
112+ with open (os .path .join (docker_container_dir , "variables.tf" ), "w" ) as module_vars_file :
113+ module_vars_file .write ('''
114+ variable "image" {
115+ description = "Docker image."
97116 type = string
98117}
99118
100119variable "name" {
101- description = "The name of the Docker container ."
120+ description = "Container name."
102121 type = string
103122}
104123
105124variable "ports" {
106- description = "List of ports for the container ."
125+ description = "List of exposed ports ."
107126 type = list(string)
108127}
128+
129+ variable "env" {
130+ description = "Map of environment variables."
131+ type = map(string)
132+ }
109133''' )
110134
111- # Create terraform.tfvars in modules/docker
112- with open (os .path .join (docker_dir , "terraform.tfvars" ), "w" ) as docker_tfvars_file :
113- docker_tfvars_file .write ('''image = "nginx:latest"
114- name = "my_nginx"
115- ports = ["80", "80"]
135+ # Create docker_container/terraform.tfvars
136+ with open (os .path .join (docker_container_dir , "terraform.tfvars" ), "w" ) as module_tfvars_file :
137+ module_tfvars_file .write ('''
138+ image = "nginx:latest"
139+ name = "my-nginx-container"
140+ ports = ["80:80"]
141+ env = { "MY_ENV_VAR" = "value" }
116142''' )
117143
118- # Create versions.tf in modules/docker
119- with open (os .path .join (docker_dir , "versions.tf" ), "w" ) as docker_versions_file :
120- docker_versions_file .write ('''terraform {
144+ # Create docker_container/versions.tf
145+ with open (os .path .join (docker_container_dir , "versions.tf" ), "w" ) as module_versions_file :
146+ module_versions_file .write ('''
147+ terraform {
121148 required_version = ">= 1.0"
122149
123150 required_providers {
124151 docker = {
125- source = "hashicorp /docker"
126- version = ">= 2.0"
152+ source = "kreuzwerker /docker"
153+ version = ">= 2.8. 0"
127154 }
128155 }
129156}
130157''' )
131158
132- # Create outputs.tf in modules/docker
133- with open (os .path .join (docker_dir , "outputs.tf" ), "w" ) as docker_outputs_file :
134- docker_outputs_file .write ('''output "container_id" {
159+ # Create docker_container/outputs.tf
160+ with open (os .path .join (docker_container_dir , "outputs.tf" ), "w" ) as module_outputs_file :
161+ module_outputs_file .write ('''
162+ output "container_id" {
135163 description = "The ID of the Docker container."
136- value = docker_container.this .id
164+ value = docker_container.app .id
137165}
138166
139167output "container_ip" {
140168 description = "The IP address of the Docker container."
141- value = docker_container.this .ip_address
169+ value = docker_container.app .ip_address
142170}
143171''' )
0 commit comments