Skip to content

Commit e6d52b0

Browse files
matifalibpmct
andauthored
docs: add template to provision docker image based workspaces on fly.io (#6526)
* add fly.io example * fix: `fly_volume` does not allow using - in name. fix: `fly_volume` does not allow using - in the name. * fix: provider versions and settings * fix: valid `fly_app` name * chore: ipv6 is not used * fix: names strangely `fly_volume` does not allow `-` and `fly_app` does not allow `_`. * chore: update max RAM * add fly-auth-api managed variables * Update README.md * improve setup flow - user is not prompted in UI for default values - org slug is best fetched via CLI * add metadata * add to starter templates --------- Co-authored-by: Ben <me@bpmct.net>
1 parent e55d921 commit e6d52b0

File tree

4 files changed

+312
-0
lines changed

4 files changed

+312
-0
lines changed

examples/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var (
3232
//go:embed templates/gcp-vm-container
3333
//go:embed templates/gcp-windows
3434
//go:embed templates/kubernetes
35+
//go:embed templates/fly-docker-image
3536
files embed.FS
3637

3738
exampleBasePath = "https://github.com/coder/coder/tree/main/examples/templates/"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
name: Develop on a Fly.io container
3+
description: Run workspaces as Firecracker VMs on Fly.io
4+
tags: [docker, fly.io]
5+
icon: /icon/fly.io.svg
6+
---
7+
8+
# Coder Fly.io Template
9+
10+
This template provisions a [code-server](https://github.com/coder/code-server) instance on [fly.io](https://fly.io) using the [codercom/code-server](https://hub.docker.com/r/codercom/code-server) image.
11+
12+
## Prerequisites
13+
14+
- [flyctl](https://fly.io/docs/getting-started/installing-flyctl/) installed.
15+
- [Coder](https://coder.com/) already setup and running with coder-cli installed locally.
16+
17+
## Getting started
18+
19+
1. Run `coder templates init` and select this template. Follow the instructions that appear.
20+
2. cd into the directory that was created. (e.g. `cd fly-docker-image`)
21+
3. Create the new template by running the following command from the `fly-docker-image` directory:
22+
23+
```bash
24+
coder templates create fly-docker-image \
25+
--variable fly_api_token=$(flyctl auth token) \
26+
--variable fly_org=personal
27+
```
28+
29+
> If the Coder server is also running as a fly.io app, then instead of setting variable `fly_api_token` you can also set a fly.io secret with the name `FLY_API_TOKEN`
30+
>
31+
> ```bash
32+
> flyctl secrets set FLY_API_TOKEN=$(flyctl auth token) --app <your-coder-app-name>
33+
> ```
34+
35+
> Read our blog [post](coder.com/blog/deploying-coder-on-fly-io) to learn more about how to deploy Coder on fly.io.
36+
37+
4. Navigate to the Coder dashboard and create a new workspace using the template.
38+
39+
This is all. You should now have a code-server instance running on fly.io.
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
terraform {
2+
required_providers {
3+
fly = {
4+
source = "fly-apps/fly"
5+
version = "~>0.0.21"
6+
}
7+
coder = {
8+
source = "coder/coder"
9+
version = "~>0.6.17"
10+
}
11+
}
12+
}
13+
14+
provider "fly" {
15+
useinternaltunnel = true
16+
internaltunnelorg = var.fly_org
17+
internaltunnelregion = data.coder_parameter.region.value
18+
fly_api_token = var.fly_api_token == "" ? null : var.fly_api_token
19+
}
20+
21+
provider "coder" {
22+
feature_use_managed_variables = true
23+
}
24+
25+
resource "fly_app" "workspace" {
26+
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
27+
org = var.fly_org
28+
}
29+
30+
resource "fly_ip" "workspace-ip4" {
31+
app = fly_app.workspace.name
32+
type = "v4"
33+
}
34+
35+
resource "fly_volume" "home-volume" {
36+
app = fly_app.workspace.name
37+
name = "coder_${data.coder_workspace.me.owner}_${lower(replace(data.coder_workspace.me.name, "-", "_"))}_home"
38+
size = data.coder_parameter.volume-size.value
39+
region = data.coder_parameter.region.value
40+
}
41+
42+
resource "fly_machine" "workspace" {
43+
count = data.coder_workspace.me.start_count
44+
app = fly_app.workspace.name
45+
region = data.coder_parameter.region.value
46+
name = data.coder_workspace.me.name
47+
image = data.coder_parameter.docker-image.value
48+
cpus = data.coder_parameter.cpu.value
49+
memorymb = data.coder_parameter.memory.value * 1024
50+
env = {
51+
CODER_AGENT_TOKEN = "${coder_agent.main.token}"
52+
}
53+
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "${fly_ip.workspace-ip4.address}")] # replace localhost with the IP of the workspace
54+
services = [
55+
{
56+
ports = [
57+
{
58+
port = 443
59+
handlers = ["tls", "http"]
60+
},
61+
{
62+
port = 80
63+
handlers = ["http"]
64+
}
65+
66+
]
67+
protocol = "tcp",
68+
"internal_port" = 80
69+
},
70+
{
71+
ports = [
72+
{
73+
port = 8080
74+
handlers = ["tls", "http"]
75+
}
76+
]
77+
protocol = "tcp",
78+
"internal_port" = 8080
79+
}
80+
]
81+
mounts = [
82+
{
83+
volume = fly_volume.home-volume.id
84+
path = "/home/coder"
85+
}
86+
]
87+
}
88+
89+
variable "fly_api_token" {
90+
type = string
91+
description = <<-EOF
92+
The Fly.io API token to use for deploying the workspace. You can generate one by running:
93+
94+
$ flyctl auth token
95+
EOF
96+
sensitive = true
97+
}
98+
99+
variable "fly_org" {
100+
type = string
101+
description = <<-EOF
102+
The Fly.io organization slug to deploy the workspace in. List organizations by running:
103+
104+
$ flyctl orgs list
105+
EOF
106+
}
107+
108+
data "coder_parameter" "docker-image" {
109+
name = "Docker Image"
110+
description = "The docker image to use for the workspace"
111+
default = "codercom/code-server:latest"
112+
icon = "https://raw.githubusercontent.com/matifali/logos/main/docker.svg"
113+
}
114+
115+
data "coder_parameter" "cpu" {
116+
name = "CPU"
117+
description = "The number of CPUs to allocate to the workspace (1-8)"
118+
type = "number"
119+
default = "1"
120+
icon = "https://raw.githubusercontent.com/matifali/logos/main/cpu-3.svg"
121+
mutable = true
122+
validation {
123+
min = 1
124+
max = 8
125+
}
126+
}
127+
128+
data "coder_parameter" "memory" {
129+
name = "Memory (GB)"
130+
description = "The amount of memory to allocate to the workspace in GB (1-16)"
131+
type = "number"
132+
default = "1"
133+
icon = "/icon/memory.svg"
134+
mutable = true
135+
validation {
136+
min = 1
137+
max = 16
138+
}
139+
}
140+
141+
data "coder_parameter" "volume-size" {
142+
name = "Volume Size"
143+
description = "The size of the volume to create for the workspace in GB (1-20)"
144+
type = "number"
145+
default = "3"
146+
icon = "https://raw.githubusercontent.com/matifali/logos/main/database.svg"
147+
validation {
148+
min = 1
149+
max = 20
150+
}
151+
}
152+
153+
# You can see all available regions here: https://fly.io/docs/reference/regions/
154+
data "coder_parameter" "region" {
155+
name = "Region"
156+
description = "The region to deploy the workspace in"
157+
default = "ams"
158+
icon = "/emojis/1f30e.png"
159+
option {
160+
name = "Amsterdam, Netherlands"
161+
value = "ams"
162+
icon = "/emojis/1f1f3-1f1f1.png"
163+
}
164+
option {
165+
name = "Frankfurt, Germany"
166+
value = "fra"
167+
icon = "/emojis/1f1e9-1f1ea.png"
168+
}
169+
option {
170+
name = "Paris, France"
171+
value = "cdg"
172+
icon = "/emojis/1f1eb-1f1f7.png"
173+
}
174+
option {
175+
name = "Denver, Colorado (US)"
176+
value = "den"
177+
icon = "/emojis/1f1fa-1f1f8.png"
178+
}
179+
option {
180+
name = "Dallas, Texas (US)"
181+
value = "dal"
182+
icon = "/emojis/1f1fa-1f1f8.png"
183+
}
184+
option {
185+
name = "Hong Kong"
186+
value = "hkg"
187+
icon = "/emojis/1f1ed-1f1f0.png"
188+
}
189+
option {
190+
name = "Los Angeles, California (US)"
191+
value = "lax"
192+
icon = "/emojis/1f1fa-1f1f8.png"
193+
}
194+
option {
195+
name = "London, United Kingdom"
196+
value = "lhr"
197+
icon = "/emojis/1f1ec-1f1e7.png"
198+
}
199+
option {
200+
name = "Chennai, India"
201+
value = "maa"
202+
icon = "/emojis/1f1ee-1f1f3.png"
203+
}
204+
option {
205+
name = "Tokyo, Japan"
206+
value = "nrt"
207+
icon = "/emojis/1f1ef-1f1f5.png"
208+
}
209+
option {
210+
name = "Chicago, Illinois (US)"
211+
value = "ord"
212+
icon = "/emojis/1f1fa-1f1f8.png"
213+
}
214+
option {
215+
name = "Seattle, Washington (US)"
216+
value = "sea"
217+
icon = "/emojis/1f1fa-1f1f8.png"
218+
}
219+
option {
220+
name = "Singapore"
221+
value = "sin"
222+
icon = "/emojis/1f1f8-1f1ec.png"
223+
}
224+
option {
225+
name = "Sydney, Australia"
226+
value = "syd"
227+
icon = "/emojis/1f1e6-1f1fa.png"
228+
}
229+
option {
230+
name = "Toronto, Canada"
231+
value = "yyz"
232+
icon = "/emojis/1f1e8-1f1e6.png"
233+
}
234+
}
235+
236+
resource "coder_app" "code-server" {
237+
count = 1
238+
agent_id = coder_agent.main.id
239+
display_name = "Code Server"
240+
slug = "code-server"
241+
url = "http://localhost:8080?folder=/home/coder/"
242+
icon = "/icon/code.svg"
243+
subdomain = false
244+
share = "owner"
245+
246+
healthcheck {
247+
url = "http://localhost:8080/healthz"
248+
interval = 3
249+
threshold = 10
250+
}
251+
}
252+
253+
resource "coder_agent" "main" {
254+
arch = data.coder_provisioner.me.arch
255+
os = "linux"
256+
login_before_ready = false
257+
startup_script_timeout = 180
258+
startup_script = <<-EOT
259+
set -e
260+
# Start code-server
261+
code-server --auth none >/tmp/code-server.log 2>&1 &
262+
# Set the hostname to the workspace name
263+
sudo hostname -b "${data.coder_workspace.me.name}-fly"
264+
EOT
265+
}
266+
267+
data "coder_provisioner" "me" {
268+
}
269+
270+
data "coder_workspace" "me" {
271+
}

site/static/icon/fly.io.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)