Skip to content

example: add and document dotfiles usage #2046

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 3 commits into from
Jun 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify template based on review
  • Loading branch information
bpmct committed Jun 6, 2022
commit a359aa67ee5449679333cecc516e92078babc57c
78 changes: 14 additions & 64 deletions examples/templates/docker-with-dotfiles/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Note: this example demonstrates the use of
# dotfiles with Coder templates.

# The Docker aspect of the template only works
# with MacOS/Linux amd64 systems. See the full
# Docker example for details

terraform {
required_providers {
coder = {
Expand All @@ -11,52 +18,8 @@ terraform {
}
}

# Admin parameters

# Comment this out if you are specifying a different docker
# host on the "docker" provider below.
variable "step1_docker_host_warning" {
description = <<-EOF
This template will use the Docker socket present on
the Coder host, which is not necessarily your local machine.

You can specify a different host in the template file and
surpress this warning.
EOF
validation {
condition = contains(["Continue using /var/run/docker.sock on the Coder host"], var.step1_docker_host_warning)
error_message = "Cancelling template create."
}

sensitive = true
}
variable "step2_arch" {
description = <<-EOF
arch: What architecture is your Docker host on?

note: codercom/enterprise-* images are only built for amd64
EOF

validation {
condition = contains(["amd64", "arm64", "armv7"], var.step2_arch)
error_message = "Value must be amd64, arm64, or armv7."
}
sensitive = true
}
variable "step3_OS" {
description = <<-EOF
What operating system is your Coder host on?
EOF

validation {
condition = contains(["MacOS", "Windows", "Linux"], var.step3_OS)
error_message = "Value must be MacOS, Windows, or Linux."
}
sensitive = true
}

provider "docker" {
host = var.step3_OS == "Windows" ? "npipe:////.//pipe//docker_engine" : "unix:///var/run/docker.sock"
host = "unix:///var/run/docker.sock"
}

provider "coder" {
Expand All @@ -65,29 +28,18 @@ provider "coder" {
data "coder_workspace" "me" {
}

variable "docker_image" {
description = "Which Docker image would you like to use for your workspace?"
# The codercom/enterprise-* images are only built for amd64
default = "codercom/enterprise-base:ubuntu"
validation {
condition = contains(["codercom/enterprise-base:ubuntu", "codercom/enterprise-node:ubuntu", "codercom/enterprise-intellij:ubuntu"], var.docker_image)
error_message = "Invalid Docker image!"
}
}

variable "dotfiles_uri" {
description = <<-EOF
Dotfiles repo URI (optional)

see https://dotfiles.github.io
EOF
# The codercom/enterprise-* images are only built for amd64
default = ""
default = ""
}

resource "coder_agent" "dev" {
arch = var.step2_arch
os = "linux"
arch = "amd64"
os = "linux"
startup_script = var.dotfiles_uri != "" ? "/tmp/tmp.coder*/coder dotfiles -y ${var.dotfiles_uri}" : null
}

Expand All @@ -97,13 +49,11 @@ resource "docker_volume" "home_volume" {

resource "docker_container" "workspace" {
count = data.coder_workspace.me.start_count
image = var.docker_image
image = "codercom/enterprise-base:ubuntu"
# Uses lower() to avoid Docker restriction on container names.
name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
# Hostname makes the shell more user friendly: coder@my-workspace:~$
hostname = lower(data.coder_workspace.me.name)
dns = ["1.1.1.1"]
# Use the docker gateway if the access URL is 127.0.0.1
dns = ["1.1.1.1"]
# Refer to Docker host when Coder is on localhost
command = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
host {
Expand Down