Skip to content

docs: update template-from-scratch #15101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 12, 2024
Merged
Next Next commit
update template main.tf
  • Loading branch information
EdwardAngert committed Nov 1, 2024
commit ff71e8320d0e2b998e27b7781b3f13beddb6dfe0
64 changes: 24 additions & 40 deletions docs/tutorials/template-from-scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ To follow this guide, you'll need:
[Docker](https://docs.docker.com/get-docker/) and [Coder](../install/index.md)
installed on it.

> When setting up your computer or computing instance, make sure to install
> Docker first, then Coder. Otherwise, you'll need to add the `coder` user to
> the `docker` group.
- When setting up your computer or computing instance, make sure to install Docker first, then Coder. Otherwise, you'll need to add the `coder` user to the `docker` group.

- The URL for your Coder instance. If you're running Coder locally, the default
URL is [http://127.0.0.1:3000](http://127.0.0.1:3000).

- A text editor. For this tour, we use [GNU nano](https://nano-editor.org/).

> Haven't written Terraform before? Check out Hashicorp's
> [Getting Started Guides](https://developer.hashicorp.com/terraform/tutorials).

## What's in a template

The main part of a Coder template is a [Terraform](https://terraform.io) `tf`
Expand All @@ -36,6 +31,9 @@ Coder can provision all Terraform modules, resources, and properties. The Coder
server essentially runs a `terraform apply` every time a workspace is created,
started, or stopped.

> Haven't written Terraform before? Check out Hashicorp's
> [Getting Started Guides](https://developer.hashicorp.com/terraform/tutorials).

Here's a simplified diagram that shows the main parts of the template we'll
create.

Expand All @@ -47,10 +45,8 @@ On your local computer, create a directory for your template and create the
`Dockerfile`.

```sh
mkdir template-tour
cd template-tour
mkdir build
nano build/Dockerfile
mkdir -p template-tour/build && cd $_
nano Dockerfile
```

You'll enter a simple `Dockerfile` that starts with the
Expand All @@ -72,7 +68,6 @@ RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \
&& chmod 0440 /etc/sudoers.d/${USER}
USER ${USER}
WORKDIR /home/${USER}

```

Notice how `Dockerfile` adds a few things to the parent `ubuntu` image, which
Expand All @@ -83,7 +78,7 @@ your template needs later:

## 2. Set up template providers

Now you can edit the Terraform file, which provisions the workspace's resources.
Edit the Terraform file to provision the workspace's resources:

```shell
nano main.tf
Expand All @@ -97,31 +92,28 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "~> 0.8.3"
}
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.1"
}
}
}

provider "coder" {
locals {
username = data.coder_workspace_owner.me.name
}

provider "docker" {
data "coder_provisioner" "me" {
}

locals {
username = data.coder_workspace.me.owner
provider "docker" {
}

data "coder_provisioner" "me" {
provider "coder" {
}

data "coder_workspace" "me" {
}

```

Notice that the `provider` blocks for `coder` and `docker` are empty. In a more
Expand All @@ -132,8 +124,7 @@ The
[`coder_workspace`](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace)
data source provides details about the state of a workspace, such as its name,
owner, and so on. The data source also lets us know when a workspace is being
started or stopped. We'll take advantage of this information in later steps to
do these things:
started or stopped. We'll take advantage of this information in later steps to:

- Set some environment variables based on the workspace owner.
- Manage ephemeral and persistent storage.
Expand All @@ -150,26 +141,25 @@ You do not need to have any open ports on the compute aspect, but the agent
needs `curl` access to the Coder server. Remember that we installed `curl` in
`Dockerfile`, earlier.

This snippet creates the agent:
Add this snippet below the last closing `}` in `main.tf` to create the agent:

```tf
resource "coder_agent" "main" {
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script_timeout = 180
startup_script = <<-EOT
set -e

# install and start code-server
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
EOT

env = {
GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
}

metadata {
Expand All @@ -188,11 +178,10 @@ resource "coder_agent" "main" {
timeout = 1
}
}

```

Because Docker is running locally in the Coder server, there is no need to
authenticate `coder_agent`. But if your `coder_agent` were running on a remote
authenticate `coder_agent`. But if your `coder_agent` is running on a remote
host, your template would need
[authentication credentials](../admin/external-auth.md).

Expand Down Expand Up @@ -229,7 +218,7 @@ This is commonly used for
[web IDEs](../user-guides/workspace-access/web-ides.md) such as
[code-server](https://coder.com/docs/code-server), RStudio, and JupyterLab.

To install and code-server in the workspace, remember that we installed it in
To install code-server in the workspace, remember that we installed it in
the `startup_script` argument in `coder_agent`. We make it available from a
workspace with a `coder_app` resource. See
[web IDEs](../user-guides/workspace-access/web-ides.md) for more examples.
Expand All @@ -250,11 +239,10 @@ resource "coder_app" "code-server" {
threshold = 6
}
}

```

You can also use a `coder_app` resource to link to external apps, such as links
to wikis or cloud consoles.
to wikis or cloud consoles:

```tf
resource "coder_app" "coder-server-doc" {
Expand All @@ -264,7 +252,6 @@ resource "coder_app" "coder-server-doc" {
url = "https://coder.com/docs/code-server"
external = true
}

```

## 5. Persistent and ephemeral resources
Expand Down Expand Up @@ -296,7 +283,6 @@ resource "docker_volume" "home_volume" {
ignore_changes = all
}
}

```

For details, see
Expand All @@ -305,7 +291,7 @@ For details, see
## 6. Set up the Docker container

To set up our Docker container, our template has a `docker_image` resource that
uses `build/Dockerfile`, which we created earlier.
uses `build/Dockerfile`, which we created earlier:

```tf
resource "docker_image" "main" {
Expand All @@ -320,7 +306,6 @@ resource "docker_image" "main" {
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
}
}

```

Our `docker_container` resource uses `coder_workspace` `start_count` to start
Expand All @@ -331,7 +316,7 @@ resource "docker_container" "workspace" {
count = data.coder_workspace.me.start_count
image = docker_image.main.name
# Uses lower() to avoid Docker restriction on container names.
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
# Hostname makes the shell more user friendly: coder@my-workspace:~$
hostname = data.coder_workspace.me.name
# Use the docker gateway if the access URL is 127.0.0.1
Expand All @@ -349,7 +334,6 @@ resource "docker_container" "workspace" {
read_only = false
}
}

```

## 7. Create the template in Coder
Expand Down