1
1
// The below example illustrates the behavior of the envbuilder_cached_image
2
2
// resource.
3
- // 1) Run a local registry:
4
- //
5
- // ```shell
6
- // docker run -d -p 5000:5000 --name test-registry registry:2
7
- // ```
8
- //
9
- // 2) Running a `terraform plan` should result in the following outputs:
10
- //
11
- // ```
12
- // + builder_image = "ghcr.io/coder/envbuilder-preview:latest"
13
- // + exists = (known after apply)
14
- // + id = (known after apply)
15
- // + image = (known after apply)
16
- // ```
17
- //
18
- // 3) Running `terraform apply` should result in outputs similar to the below:
19
- //
20
- // ```
21
- // builder_image = "ghcr.io/coder/envbuilder-preview:latest"
22
- // exists = false
23
- // id = "00000000-0000-0000-0000-000000000000"
24
- // image = "ghcr.io/coder/envbuilder-preview:latest"
25
- // ```
26
- //
27
- // 4) Populate the cache by running Envbuilder and pushing the built image to
28
- // the local registry:
29
- //
30
- // ```shell
31
- // docker run -it --rm \
32
- // -e ENVBUILDER_CACHE_REPO=localhost:5000/test \
33
- // -e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder-starter-devcontainer \
34
- // -e ENVBUILDER_PUSH_IMAGE=true \
35
- // -e ENVBUILDER_INIT_SCRIPT=exit \
36
- // --net=host \
37
- // ghcr.io/coder/envbuilder-preview:latest
38
- // ```
39
- //
40
- // 5) Run `terraform plan` once more. Now, the cached image will be detected:
41
- //
42
- // ```
43
- // Note: Objects have changed outside of Terraform
44
- //
45
- // Terraform detected the following changes made outside of Terraform since the last "terraform apply" which may have affected this plan:
46
- // envbuilder_cached_image.example has been deleted
47
- // - resource "envbuilder_cached_image" "example" {
48
- // - exists = false -> null
49
- // - id = "00000000-0000-0000-0000-000000000000" -> null
50
- // - image = "ghcr.io/coder/envbuilder-preview:latest" -> null
51
- // # (5 unchanged attributes hidden)
52
- // ```
53
- //
54
- // 6) Run `terraform apply` and the newly pushed image will be saved in the Terraform state:
55
- // ```shell
56
- // builder_image = "ghcr.io/coder/envbuilder-preview:latest"
57
- // exists = true
58
- // id = "sha256:xxx..."
59
- // image = "localhost:5000/test@sha256:xxx..."
60
- // ```
61
3
62
4
terraform {
63
5
required_providers {
64
6
envbuilder = {
65
7
source = " coder/envbuilder"
66
8
}
9
+ docker = {
10
+ source = " kreuzwerker/docker"
11
+ }
12
+ }
13
+ }
14
+
15
+ // This variable designates the devcontainer repo to build.
16
+ // The default is recommended for testing, as it builds relatively quickly!
17
+ variable "repo_url" {
18
+ type = string
19
+ default = " https://github.com/coder/envbuilder-starter-devcontainer"
20
+ }
21
+
22
+ // This variable designates the builder image to use to build the devcontainer.
23
+ variable "builder_image" {
24
+ type = string
25
+ default = " ghcr.io/coder/envbuilder:latest"
26
+ }
27
+
28
+ // If you have an existing repository you want to use as a cache, you can set this here.
29
+ // Otherwise, we will stand up a temporary local registry.
30
+ variable "cache_repo" {
31
+ type = string
32
+ default = " "
33
+ }
34
+
35
+ locals {
36
+ // If no registry is specified, use a default.
37
+ cache_repo = var. cache_repo == " " ? " localhost:5000/test" : var. cache_repo
38
+ }
39
+
40
+ // Start a local registry if no cache repo is specified.
41
+ resource "docker_container" "registry" {
42
+ count = var. cache_repo == " " ? 1 : 0
43
+ name = " envbuilder-cached-image-registry"
44
+ image = " registry:2"
45
+ ports {
46
+ internal = 5000
47
+ external = 5000
48
+ }
49
+ network_mode = " host"
50
+ lifecycle {
51
+ // We want to persist this across invocations
52
+ ignore_changes = all
67
53
}
68
54
}
69
55
56
+ // This resource performs the heavy lifting of determining
57
+ // if we need to build the devcontainer from scratch, or if
58
+ // there is a previously built image we can re-use.
59
+ // It fetches the Git repo located at var.git_repo, and
60
+ // performs a 'dry-run' build of the Devcontainer/Dockerfile.
61
+ // If all of the layers produced by the dry run are present
62
+ // in the remote cache repo, that image can then be used
63
+ // instead. Otherwise, the cache is stale and the image needs
64
+ // to be rebuilt.
70
65
resource "envbuilder_cached_image" "example" {
71
- builder_image = " ghcr.io/coder/envbuilder-preview:latest "
72
- git_url = " https://github.com/coder/envbuilder-starter-devcontainer "
73
- cache_repo = " localhost:5000/test "
66
+ builder_image = var . builder_image
67
+ git_url = var . repo_url
68
+ cache_repo = local . cache_repo
74
69
extra_env = {
75
70
" ENVBUILDER_VERBOSE" : " true"
71
+ " ENVBUILDER_INSECURE" : " true" # due to local registry
72
+ " ENVBUILDER_INIT_SCRIPT" : " sleep infinity"
73
+ " ENVBUILDER_PUSH_IMAGE" : " true"
76
74
}
75
+ depends_on = [docker_container . registry ]
76
+ }
77
+
78
+ // Run the cached image. Depending on the contents of
79
+ // the cache repo, this will either be var.builder_image
80
+ // or a previously built image pusehd to var.cache_repo.
81
+ // Running `terraform apply` once (assuming empty cache)
82
+ // will result in the builder image running, and the built
83
+ // image being pushed to the cache repo.
84
+ // Running `terraform apply` again will result in the
85
+ // previously built image running.
86
+ resource "docker_container" "example" {
87
+ name = " envbuilder-cached-image-example"
88
+ image = envbuilder_cached_image. example . image
89
+ env = envbuilder_cached_image. example . env
90
+ network_mode = " host" # required to hit local registry
77
91
}
78
92
79
93
output "builder_image" {
@@ -91,3 +105,4 @@ output "id" {
91
105
output "image" {
92
106
value = envbuilder_cached_image. example . image
93
107
}
108
+
0 commit comments