11import os
2+
23project_name = "app/media/MyTerraform"
34modules_dir = os .path .join (project_name , "modules" )
4- docker_container_dir = os .path .join (modules_dir , "docker_container " )
5+ docker_dir = os .path .join (modules_dir , "docker " )
56
67# Create project directories
7- os .makedirs (docker_container_dir , exist_ok = True )
8+ os .makedirs (docker_dir , exist_ok = True )
89
910# Create main.tf
1011with open (os .path .join (project_name , "main.tf" ), "w" ) as main_file :
1112 main_file .write ('''
1213provider "docker" {
13- host = var.docker_host
14+ host = "unix:/// var/run/docker.sock"
1415}
1516
16- module "docker_container" {
17- source = "./modules/docker_container"
17+ module "docker" {
18+ source = "./modules/docker"
19+
20+ image_name = var.image_name
21+ image_force_remove = var.image_force_remove
22+ image_build = var.image_build
23+ image_count = var.image_count
1824
19- image = var.image
20- name = var.name
21- ports = var.ports
22- env = var.env
25+ container_image = var.container_image
26+ container_name = var.container_name
27+ container_hostname = var.container_hostname
28+ container_restart = var.container_restart
29+ container_count = var.container_count
2330}
24-
2531''' )
2632
2733# 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."
32- type = string
34+ with open (os .path .join (project_name , "variables.tf" ), "w" ) as variables_file :
35+ variables_file .write ('''
36+ variable "image_name" {
37+ type = string
38+ }
39+
40+ variable "image_force_remove" {
41+ type = bool
42+ }
43+
44+ variable "image_build" {
45+ type = object({
46+ context = string
47+ tag = list(string)
48+ })
49+ }
50+
51+ variable "image_count" {
52+ type = number
3353}
3454
35- variable "image" {
36- description = "Docker image to use."
37- type = string
55+ variable "container_image" {
56+ type = string
3857}
3958
40- variable "name" {
41- description = "Name of the container."
42- type = string
59+ variable "container_name" {
60+ type = string
4361}
4462
45- variable "ports" {
46- description = "List of ports to expose."
47- type = list(string)
63+ variable "container_hostname" {
64+ type = string
4865}
4966
50- variable "env" {
51- description = "Environment variables for the container."
52- type = map(string)
67+ variable "container_restart" {
68+ type = string
69+ }
70+
71+ variable "container_count" {
72+ type = number
5373}
5474''' )
5575
5676# Create terraform.tfvars
5777with open (os .path .join (project_name , "terraform.tfvars" ), "w" ) as tfvars_file :
5878 tfvars_file .write ('''
59- docker_host = "tcp://localhost:2375"
60- image = "nginx:latest"
61- name = "my-nginx-container"
62- ports = ["80:80"]
63- env = { "MY_ENV_VAR" = "value" }
79+ image_name = "my-image"
80+ image_force_remove = true
81+ image_build = {
82+ context = "./"
83+ tag = ["my-image:latest"]
84+ }
85+ image_count = 1
86+
87+ container_image = "my-image"
88+ container_name = "my-container"
89+ container_hostname = "my-host"
90+ container_restart = "always"
91+ container_count = 1
6492''' )
6593
6694# Create versions.tf
76104 }
77105 }
78106}
79-
80107''' )
81108
82- # Create outputs.tf
83- with open (os .path .join (project_name , "outputs.tf" ), "w" ) as outputs_file :
84- outputs_file .write ('''
85- output "container_id" {
86- description = "The ID of the Docker container."
87- value = module.docker_container.container_id
109+ # Create module main.tf
110+ with open (os .path .join (docker_dir , "main.tf" ), "w" ) as module_main_file :
111+ module_main_file .write ('''
112+ resource "docker_image" "app_image" {
113+ count = var.image_count
114+ name = var.image_name
115+ force_remove = var.image_force_remove
116+
117+ build {
118+ context = var.image_build.context
119+ tag = var.image_build.tag
120+ }
88121}
89122
90- output "container_ip" {
91- description = "The IP address of the Docker container."
92- value = module.docker_container.container_ip
123+ resource "docker_container" "app_container" {
124+ count = var.container_count
125+ image = var.container_image
126+ name = var.container_name
127+ hostname = var.container_hostname
128+ restart = var.container_restart
93129}
94130''' )
95131
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" {
101- image = var.image
102- name = var.name
103- ports {
104- internal = 80
105- external = var.ports[0]
106- }
107- env = var.env
132+ # Create module variables.tf
133+ with open (os .path .join (docker_dir , "variables.tf" ), "w" ) as module_variables_file :
134+ module_variables_file .write ('''
135+ variable "image_name" {
136+ type = string
137+ }
138+
139+ variable "image_force_remove" {
140+ type = bool
141+ }
142+
143+ variable "image_build" {
144+ type = object({
145+ context = string
146+ tag = list(string)
147+ })
148+ }
149+
150+ variable "image_count" {
151+ type = number
108152}
109- ''' )
110153
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."
116- type = string
154+ variable "container_image" {
155+ type = string
117156}
118157
119- variable "name" {
120- description = "Container name."
121- type = string
158+ variable "container_name" {
159+ type = string
122160}
123161
124- variable "ports" {
125- description = "List of exposed ports."
126- type = list(string)
162+ variable "container_hostname" {
163+ type = string
127164}
128165
129- variable "env" {
130- description = "Map of environment variables."
131- type = map(string)
166+ variable "container_restart" {
167+ type = string
168+ }
169+
170+ variable "container_count" {
171+ type = number
132172}
133173''' )
134174
135- # Create docker_container/ terraform.tfvars
136- with open (os .path .join (docker_container_dir , "terraform.tfvars" ), "w" ) as module_tfvars_file :
175+ # Create module terraform.tfvars
176+ with open (os .path .join (docker_dir , "terraform.tfvars" ), "w" ) as module_tfvars_file :
137177 module_tfvars_file .write ('''
138- image = "nginx:latest"
139- name = "my-nginx-container"
140- ports = ["80:80"]
141- env = { "MY_ENV_VAR" = "value" }
178+ image_name = "my-image"
179+ image_force_remove = true
180+ image_build = {
181+ context = "./"
182+ tag = ["my-image:latest"]
183+ }
184+ image_count = 1
185+
186+ container_image = "my-image"
187+ container_name = "my-container"
188+ container_hostname = "my-host"
189+ container_restart = "always"
190+ container_count = 1
142191''' )
143192
144- # Create docker_container/ versions.tf
145- with open (os .path .join (docker_container_dir , "versions.tf" ), "w" ) as module_versions_file :
193+ # Create module versions.tf
194+ with open (os .path .join (docker_dir , "versions.tf" ), "w" ) as module_versions_file :
146195 module_versions_file .write ('''
147196terraform {
148197 required_version = ">= 1.0"
154203 }
155204 }
156205}
157- ''' )
158-
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" {
163- description = "The ID of the Docker container."
164- value = docker_container.app.id
165- }
166-
167- output "container_ip" {
168- description = "The IP address of the Docker container."
169- value = docker_container.app.ip_address
170- }
171206''' )
0 commit comments