Skip to content

Commit 860f7a3

Browse files
committed
example: add and document dotfiles usage
1 parent d8c4401 commit 860f7a3

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

docs/workspaces.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ resources](./templates.md#persistent-and-ephemeral-resources).
6565
6666
When a workspace is deleted, all of the workspace's resources are deleted.
6767

68+
## Dotfiles
69+
70+
Users can install configuration from a personal [dotfiles repository](https://dotfiles.github.io) with the `coder dotfiles <repo>`
71+
command in their workspace. Templates can also prompt users for their dotfiles repo [(example)](../examples/templates/docker-with-dotfiles/README.md#how-it-works).
72+
6873
## Updating workspaces
6974

7075
Use the following command to update a workspace to the latest template version.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Develop in Docker with a dotfiles URL
3+
description: Run workspaces on a Docker host using registry images
4+
tags: [local, docker]
5+
---
6+
7+
# docker-with-dotfiles
8+
9+
This is an example for deploying workspaces with a prompt for the users' dotfiles repo URI.
10+
11+
## Getting started
12+
13+
Run `coder templates init` and select this template. Follow the instructions that appear.
14+
15+
## How it works
16+
17+
During workspace creation, Coder prompts you to specify a dotfiles URL via a Terraform variable. Once the workspace starts, the Coder agent runs `coder dotfiles` via the startup script:
18+
19+
```hcl
20+
variable "dotfiles_uri" {
21+
description = <<-EOF
22+
Dotfiles repo URI (optional)
23+
24+
see https://dotfiles.github.io
25+
EOF
26+
# The codercom/enterprise-* images are only built for amd64
27+
default = ""
28+
}
29+
30+
resource "coder_agent" "dev" {
31+
...
32+
startup_script = var.dotfiles_uri != "" ? "/tmp/tmp.coder*/coder dotfiles -y ${var.dotfiles_uri}" : null
33+
}
34+
```
35+
36+
# Managing images and workspaces
37+
38+
Refer to the documentation in the [Docker template](../docker/README.md).
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "0.4.1"
6+
}
7+
docker = {
8+
source = "kreuzwerker/docker"
9+
version = "~> 2.16.0"
10+
}
11+
}
12+
}
13+
14+
# Admin parameters
15+
16+
# Comment this out if you are specifying a different docker
17+
# host on the "docker" provider below.
18+
variable "step1_docker_host_warning" {
19+
description = <<-EOF
20+
This template will use the Docker socket present on
21+
the Coder host, which is not necessarily your local machine.
22+
23+
You can specify a different host in the template file and
24+
surpress this warning.
25+
EOF
26+
validation {
27+
condition = contains(["Continue using /var/run/docker.sock on the Coder host"], var.step1_docker_host_warning)
28+
error_message = "Cancelling template create."
29+
}
30+
31+
sensitive = true
32+
}
33+
variable "step2_arch" {
34+
description = <<-EOF
35+
arch: What architecture is your Docker host on?
36+
37+
note: codercom/enterprise-* images are only built for amd64
38+
EOF
39+
40+
validation {
41+
condition = contains(["amd64", "arm64", "armv7"], var.step2_arch)
42+
error_message = "Value must be amd64, arm64, or armv7."
43+
}
44+
sensitive = true
45+
}
46+
variable "step3_OS" {
47+
description = <<-EOF
48+
What operating system is your Coder host on?
49+
EOF
50+
51+
validation {
52+
condition = contains(["MacOS", "Windows", "Linux"], var.step3_OS)
53+
error_message = "Value must be MacOS, Windows, or Linux."
54+
}
55+
sensitive = true
56+
}
57+
58+
provider "docker" {
59+
host = var.step3_OS == "Windows" ? "npipe:////.//pipe//docker_engine" : "unix:///var/run/docker.sock"
60+
}
61+
62+
provider "coder" {
63+
}
64+
65+
data "coder_workspace" "me" {
66+
}
67+
68+
variable "docker_image" {
69+
description = "Which Docker image would you like to use for your workspace?"
70+
# The codercom/enterprise-* images are only built for amd64
71+
default = "codercom/enterprise-base:ubuntu"
72+
validation {
73+
condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu", "codercom/enterprise-intellij:ubuntu"], var.docker_image)
74+
error_message = "Invalid Docker image!"
75+
}
76+
}
77+
78+
variable "dotfiles_uri" {
79+
description = <<-EOF
80+
Dotfiles repo URI (optional)
81+
82+
see https://dotfiles.github.io
83+
EOF
84+
# The codercom/enterprise-* images are only built for amd64
85+
default = ""
86+
}
87+
88+
resource "coder_agent" "dev" {
89+
arch = var.step2_arch
90+
os = "linux"
91+
startup_script = var.dotfiles_uri != "" ? "/tmp/tmp.coder*/coder dotfiles -y ${var.dotfiles_uri}" : null
92+
}
93+
94+
resource "docker_volume" "home_volume" {
95+
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-root"
96+
}
97+
98+
resource "docker_container" "workspace" {
99+
count = data.coder_workspace.me.start_count
100+
image = var.docker_image
101+
# Uses lower() to avoid Docker restriction on container names.
102+
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
103+
# Hostname makes the shell more user friendly: coder@my-workspace:~$
104+
hostname = lower(data.coder_workspace.me.name)
105+
dns = ["1.1.1.1"]
106+
# Use the docker gateway if the access URL is 127.0.0.1
107+
command = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
108+
env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
109+
host {
110+
host = "host.docker.internal"
111+
ip = "host-gateway"
112+
}
113+
volumes {
114+
container_path = "/home/coder/"
115+
volume_name = docker_volume.home_volume.name
116+
read_only = false
117+
}
118+
}

0 commit comments

Comments
 (0)