-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
220 lines (178 loc) · 4.95 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Define the provider (e.g., AWS, GCP, or local)
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.2"
}
}
}
provider "docker" {
host = "unix:///var/run/docker.sock"
}
resource "random_string" "random" {
length = 32
special = true
override_special = "/@£$"
}
# Create a network for the containers to communicate over
resource "docker_network" "staffshare" {
name = "staffshare"
}
# Run the MongoDB container
resource "docker_container" "mongodb" {
name = "mongo"
networks_advanced {
name = docker_network.staffshare.name
aliases = ["mongo"]
}
provisioner "local-exec" {
command = <<EOF
mkdir -p ${abspath(path.module)}/data/db
EOF
}
volumes {
container_path = "/data/db"
host_path = "${abspath(path.module)}/data/db"
read_only = false
}
image = var.mongodb_image
ports {
internal = 27017
external = 27017
}
restart = "always"
must_run = true
depends_on = [docker_network.staffshare]
}
resource "docker_image" "backend" {
name = var.staffshare_backend_image
build {
context = var.staffshare_backend_dir
tag = [var.staffshare_backend_image]
no_cache = true
}
force_remove = false
}
resource "docker_image" "frontend" {
name = var.staffshare_frontend_image
build {
context = var.staffshare_frontend_dir
tag = [var.staffshare_frontend_image]
no_cache = true
}
force_remove = false
}
resource "docker_image" "loadbalancer" {
name = "nginx:latest"
keep_locally = false
force_remove = false
build {
context = var.staffshare_loadbalancer_dir
tag = ["nginx:latest"]
no_cache = true
}
}
# Run the backend container
resource "docker_container" "staffshare-backend" {
name = var.staffshare_backend
image = var.staffshare_backend_image
networks_advanced {
name = docker_network.staffshare.name
aliases = ["staffshare"]
}
ports {
internal = var.staffshare_backend_port
external = var.staffshare_backend_port
}
volumes {
container_path = "/staffshare_server/src"
host_path = abspath(var.staffshare_backend_dir)
read_only = false
}
restart = "always"
must_run = true
env = [
"STAFFSHARE_SERVER_ADDR=0.0.0.0",
"STAFFSHARE_SERVER_PORT=${var.staffshare_backend_port}",
"JWT_SECRET=${var.staffshare_jwt_secret}",
"STAFFSHARE_EMAIL_ADDRESS=${var.staffshare_email_address}",
"STAFFSHARE_EMAIL_PASSWORD=${var.staffshare_email_password}",
"MONGODB_URI=mongodb://mongo:27017",
"DB_NAME=staffshare"
]
depends_on = [docker_network.staffshare, docker_image.backend, docker_container.mongodb]
}
# Run the frontend container
resource "docker_container" "staffshare-frontend" {
name = var.staffshare_frontend
image = var.staffshare_frontend_image
networks_advanced {
name = docker_network.staffshare.name
aliases = ["staffshare"]
}
volumes {
container_path = "/app"
host_path = abspath(var.staffshare_frontend_dir)
read_only = false
}
ports {
internal = var.staffshare_frontend_port
external = var.staffshare_frontend_port
}
env = [
"NEXT_PUBLIC_API_URL=https://test.staffshare.co",
"NEXTAUTH_URL=https://test.staffshare.co",
"NEXTAUTH_SECRET=${var.staffshare_jwt_secret}",
"GOOGLE_CLIENT_ID=${var.staffshare_google_client_id}",
"GOOGLE_CLIENT_SECRET=${var.staffshare_google_client_secret}",
]
restart = "always"
must_run = true
depends_on = [docker_network.staffshare, docker_image.frontend]
}
output "network_ips" {
value = docker_container.staffshare-backend.network_data[0].ip_address
}
locals {
backend_ip = docker_container.staffshare-backend.network_data[0].ip_address
frontend_ip = docker_container.staffshare-frontend.network_data[0].ip_address
}
# Run the loadbalancer container
resource "docker_container" "staffshare-loadbalancer" {
name = "staffshare-loadbalancer"
image = "nginx:latest"
networks_advanced {
name = docker_network.staffshare.name
aliases = ["staffshare"]
}
ports {
internal = var.staffshare_loadbalancer_port
external = var.staffshare_loadbalancer_port
}
env = [
"BACKEND_SERVER_IP=${local.backend_ip}",
"BACKEND_SERVER_PORT=${var.staffshare_backend_port}",
"FRONTEND_SERVER_IP=${local.frontend_ip}",
"FRONTEND_SERVER_PORT=${var.staffshare_frontend_port}",
"LOAD_BALANCER_PORT=${var.staffshare_loadbalancer_port}"
]
restart = "always"
must_run = true
depends_on = [docker_network.staffshare, docker_container.staffshare-backend, docker_container.staffshare-frontend]
}
resource "docker_container" "mongo-express" {
name = "mongo-express"
networks_advanced {
name = docker_network.staffshare.name
aliases = ["mongo-express"]
}
image = "mongo-express:latest"
ports {
internal = 8081
external = 8081
}
restart = "always"
must_run = true
depends_on = [docker_container.mongodb]
}