Skip to content

Commit 3f37703

Browse files
committed
updates after testing with new provider version
1 parent e40dd7a commit 3f37703

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

examples/templates/devcontainer-docker/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ Coder supports Devcontainers via [envbuilder](https://github.com/coder/envbuilde
3434

3535
This template provisions the following resources:
3636

37+
- Envbuilder cached image (conditional, persistent)
3738
- Docker image (persistent)
3839
- Docker container (ephemeral)
3940
- Docker volume (persistent on `/workspaces`)
4041

41-
with [`envbuilder`](https://github.com/coder/envbuilder).
42+
with [`envbuilder`](https://github.com/coder/envbuilder) and [`terraform-provider-envbuilder`](https://github.com/coder/terraform-provider-envbuilder).
4243
The Git repository is cloned inside the `/workspaces` volume if not present.
4344
Any local changes to the Devcontainer files inside the volume will be applied when you restart the workspace.
4445
Keep in mind that any tools or files outside of `/workspaces` or not added as part of the Devcontainer specification are not persisted.
@@ -51,10 +52,11 @@ Edit the `devcontainer.json` instead!
5152

5253
See the [Envbuilder documentation](https://github.com/coder/envbuilder/blob/main/docs/docker.md) for information on running Docker containers inside a devcontainer built by Envbuilder.
5354

55+
5456
## Caching
5557

5658
To speed up your builds, you can use a container registry as a cache.
57-
When creating the template, set the parameter `cache_repo`.
59+
When creating the template, set the parameter `cache_repo` to a valid Docker repository.
5860

5961
For example, you can run a local registry:
6062

@@ -69,6 +71,8 @@ docker run --detach \
6971

7072
Then, when creating the template, enter `localhost:5000/devcontainer-cache` for the parameter `cache_repo`.
7173

74+
See the [Envbuilder Terraform Provider Examples](https://github.com/coder/terraform-provider-envbuilder/blob/main/examples/resources/envbuilder_cached_image/envbuilder_cached_image_resource.tf/) for a more complete example of how the provider works.
75+
7276
> [!NOTE] We recommend using a registry cache with authentication enabled.
7377
> To allow Envbuilder to authenticate with the registry cache, specify the variable `cache_repo_docker_config_path`
7478
> with the path to a Docker config `.json` on disk containing valid credentials for the registry.

examples/templates/devcontainer-docker/main.tf

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,13 @@ EOF
9393

9494
variable "cache_repo" {
9595
default = ""
96-
description = "Use a container registry as a cache to speed up builds."
97-
sensitive = true
96+
description = "(Optional) Use a container registry as a cache to speed up builds."
9897
type = string
9998
}
10099

101100
variable "cache_repo_docker_config_path" {
102101
default = ""
103-
description = "Path to a docker config.json containing credentials to the provided cache repo, if required."
102+
description = "(Optional) Path to a docker config.json containing credentials to the provided cache repo, if required."
104103
sensitive = true
105104
type = string
106105
}
@@ -152,15 +151,15 @@ resource "docker_volume" "workspaces" {
152151
# Check for the presence of a prebuilt image in the cache repo
153152
# that we can use instead.
154153
resource "envbuilder_cached_image" "cached" {
155-
count = data.coder_workspace.me.start_count
154+
count = var.cache_repo == "" ? 0 : data.coder_workspace.me.start_count
156155
builder_image = local.devcontainer_builder_image
157156
git_url = local.repo_url
158157
cache_repo = var.cache_repo
159158
}
160159

161160
resource "docker_container" "workspace" {
162161
count = data.coder_workspace.me.start_count
163-
image = envbuilder_cached_image.cached.0.image
162+
image = var.cache_repo == "" ? local.devcontainer_builder_image : envbuilder_cached_image.cached.0.image
164163
# Uses lower() to avoid Docker restriction on container names.
165164
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
166165
# Hostname makes the shell more user friendly: coder@my-workspace:~$
@@ -174,10 +173,10 @@ resource "docker_container" "workspace" {
174173
"ENVBUILDER_FALLBACK_IMAGE=${data.coder_parameter.fallback_image.value}",
175174
"ENVBUILDER_CACHE_REPO=${var.cache_repo}",
176175
"ENVBUILDER_DOCKER_CONFIG_BASE64=${try(data.local_sensitive_file.cache_repo_dockerconfigjson[0].content_base64, "")}",
177-
"ENVBUILDER_PUSH_IMAGE=${var.cache_repo != "" ? "true" : ""}",
178-
#"ENVBUILDER_INSECURE=true", # Uncomment if testing with a local registry.
176+
"ENVBUILDER_PUSH_IMAGE=${var.cache_repo == "" ? "" : "true"}",
177+
#"ENVBUILDER_INSECURE=true", # Uncomment if testing with a registry running on `localhost`.
179178
]
180-
# network_mode = "host" # Uncomment if testing with a local registry.
179+
# network_mode = "host" # Uncomment if testing with a registry running on `localhost`.
181180
host {
182181
host = "host.docker.internal"
183182
ip = "host-gateway"
@@ -314,3 +313,20 @@ resource "coder_app" "code-server" {
314313
threshold = 6
315314
}
316315
}
316+
317+
resource "coder_metadata" "container_info" {
318+
count = data.coder_workspace.me.start_count
319+
resource_id = docker_container.workspace.0.id
320+
item {
321+
key = "workspace image"
322+
value = var.cache_repo == "" ? local.devcontainer_builder_image : envbuilder_cached_image.cached.0.image
323+
}
324+
item {
325+
key = "git url"
326+
value = local.repo_url
327+
}
328+
item {
329+
key = "cache repo"
330+
value = var.cache_repo == "" ? "not enabled" : var.cache_repo
331+
}
332+
}

0 commit comments

Comments
 (0)