Skip to content

Commit 903f5ff

Browse files
committed
add expand of full main.tf
1 parent 5ae09f6 commit 903f5ff

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

docs/tutorials/template-from-scratch.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,137 @@ resource "docker_container" "workspace" {
338338
}
339339
```
340340

341+
<details>
342+
<summary>Expand for the full `main.tf` file</summary>
343+
344+
```tf
345+
terraform {
346+
required_providers {
347+
coder = {
348+
source = "coder/coder"
349+
}
350+
docker = {
351+
source = "kreuzwerker/docker"
352+
}
353+
}
354+
}
355+
356+
locals {
357+
username = data.coder_workspace_owner.me.name
358+
}
359+
360+
data "coder_provisioner" "me" {
361+
}
362+
363+
provider "docker" {
364+
}
365+
366+
provider "coder" {
367+
}
368+
369+
data "coder_workspace" "me" {
370+
}
371+
372+
data "coder_workspace_owner" "me" {}
373+
374+
resource "coder_agent" "main" {
375+
arch = data.coder_provisioner.me.arch
376+
os = "linux"
377+
startup_script = <<-EOT
378+
set -e
379+
380+
# install and start code-server
381+
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
382+
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
383+
EOT
384+
385+
env = {
386+
GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
387+
GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
388+
GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
389+
GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
390+
}
391+
392+
metadata {
393+
display_name = "CPU Usage"
394+
key = "0_cpu_usage"
395+
script = "coder stat cpu"
396+
interval = 10
397+
timeout = 1
398+
}
399+
400+
metadata {
401+
display_name = "RAM Usage"
402+
key = "1_ram_usage"
403+
script = "coder stat mem"
404+
interval = 10
405+
timeout = 1
406+
}
407+
}
408+
409+
resource "coder_app" "code-server" {
410+
agent_id = coder_agent.main.id
411+
slug = "code-server"
412+
display_name = "code-server"
413+
url = "http://localhost:13337/?folder=/home/${local.username}"
414+
icon = "/icon/code.svg"
415+
subdomain = false
416+
share = "owner"
417+
418+
healthcheck {
419+
url = "http://localhost:13337/healthz"
420+
interval = 5
421+
threshold = 6
422+
}
423+
}
424+
425+
resource "docker_volume" "home_volume" {
426+
name = "coder-${data.coder_workspace.me.id}-home"
427+
# Protect the volume from being deleted due to changes in attributes.
428+
lifecycle {
429+
ignore_changes = all
430+
}
431+
}
432+
433+
resource "docker_image" "main" {
434+
name = "coder-${data.coder_workspace.me.id}"
435+
build {
436+
context = "./build"
437+
build_args = {
438+
USER = local.username
439+
}
440+
}
441+
triggers = {
442+
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
443+
}
444+
}
445+
446+
resource "docker_container" "workspace" {
447+
count = data.coder_workspace.me.start_count
448+
image = docker_image.main.name
449+
# Uses lower() to avoid Docker restriction on container names.
450+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
451+
# Hostname makes the shell more user friendly: coder@my-workspace:~$
452+
hostname = data.coder_workspace.me.name
453+
# Use the docker gateway if the access URL is 127.0.0.1
454+
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
455+
env = [
456+
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
457+
]
458+
host {
459+
host = "host.docker.internal"
460+
ip = "host-gateway"
461+
}
462+
volumes {
463+
container_path = "/home/${local.username}"
464+
volume_name = docker_volume.home_volume.name
465+
read_only = false
466+
}
467+
}
468+
```
469+
470+
</details>
471+
341472
## 7. Create the template in Coder
342473

343474
Save `main.tf` and exit the editor.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy