From 8bfaecd068f05c4941f7220889f114bc2831ce9a Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Thu, 17 Jul 2025 13:18:04 +0000 Subject: [PATCH 1/2] Add docker-build template to coder-labs namespace This template builds Docker containers from a Dockerfile instead of using a pre-built image, allowing for more customization of the development environment. Based on the docker template that was removed in coder/coder#15504. Co-authored-by: matifali <10648092+matifali@users.noreply.github.com> --- .../templates/docker-build/README.md | 59 +++++ .../templates/docker-build/build/Dockerfile | 18 ++ .../coder-labs/templates/docker-build/main.tf | 222 ++++++++++++++++++ 3 files changed, 299 insertions(+) create mode 100644 registry/coder-labs/templates/docker-build/README.md create mode 100644 registry/coder-labs/templates/docker-build/build/Dockerfile create mode 100644 registry/coder-labs/templates/docker-build/main.tf diff --git a/registry/coder-labs/templates/docker-build/README.md b/registry/coder-labs/templates/docker-build/README.md new file mode 100644 index 00000000..4c05d72b --- /dev/null +++ b/registry/coder-labs/templates/docker-build/README.md @@ -0,0 +1,59 @@ +--- +display_name: Docker Build +description: Build Docker containers from Dockerfile as Coder workspaces +icon: ../../../../.icons/docker.svg +maintainer_github: coder +verified: true +tags: [docker, container, build, dockerfile] +--- + +# Remote Development on Docker Containers (Build from Dockerfile) + +Build and provision Docker containers from a Dockerfile as [Coder workspaces](https://coder.com/docs/workspaces) with this example template. + +This template builds a custom Docker image from the included Dockerfile, allowing you to customize the development environment by modifying the Dockerfile rather than using a pre-built image. + + + +## Prerequisites + +### Infrastructure + +The VM you run Coder on must have a running Docker socket and the `coder` user must be added to the Docker group: + +```sh +# Add coder user to Docker group +sudo adduser coder docker + +# Restart Coder server +sudo systemctl restart coder + +# Test Docker +sudo -u coder docker ps +``` + +## Architecture + +This template provisions the following resources: + +- Docker image (built from Dockerfile and kept locally) +- Docker container pod (ephemeral) +- Docker volume (persistent on `/home/coder`) + +This means, when the workspace restarts, any tools or files outside of the home directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the `build/Dockerfile`. Alternatively, individual developers can [personalize](https://coder.com/docs/dotfiles) their workspaces with dotfiles. + +> **Note** +> This template is designed to be a starting point! Edit the Terraform and Dockerfile to extend the template to support your use case. + +### Editing the image + +Edit the `build/Dockerfile` and run `coder templates push` to update workspaces. The image will be rebuilt automatically when the Dockerfile changes. + +## Difference from the standard Docker template + +The main difference between this template and the standard Docker template is: + +- **Standard Docker template**: Uses a pre-built image (e.g., `codercom/enterprise-base:ubuntu`) +- **Docker Build template**: Builds a custom image from the included `build/Dockerfile` + +This allows for more customization of the development environment while maintaining the same workspace functionality. diff --git a/registry/coder-labs/templates/docker-build/build/Dockerfile b/registry/coder-labs/templates/docker-build/build/Dockerfile new file mode 100644 index 00000000..a443b5d0 --- /dev/null +++ b/registry/coder-labs/templates/docker-build/build/Dockerfile @@ -0,0 +1,18 @@ +FROM ubuntu + +RUN apt-get update \ + && apt-get install -y \ + curl \ + git \ + golang \ + sudo \ + vim \ + wget \ + && rm -rf /var/lib/apt/lists/* + +ARG USER=coder +RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \ + && echo "${USER} ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/${USER} \ + && chmod 0440 /etc/sudoers.d/${USER} +USER ${USER} +WORKDIR /home/${USER} diff --git a/registry/coder-labs/templates/docker-build/main.tf b/registry/coder-labs/templates/docker-build/main.tf new file mode 100644 index 00000000..4af9318e --- /dev/null +++ b/registry/coder-labs/templates/docker-build/main.tf @@ -0,0 +1,222 @@ +terraform { + required_providers { + coder = { + source = "coder/coder" + } + docker = { + source = "kreuzwerker/docker" + } + } +} + +locals { + username = data.coder_workspace_owner.me.name +} + +variable "docker_socket" { + default = "" + description = "(Optional) Docker socket URI" + type = string +} + +provider "docker" { + # Defaulting to null if the variable is an empty string lets us have an optional variable without having to set our own default + host = var.docker_socket != "" ? var.docker_socket : null +} + +data "coder_provisioner" "me" {} +data "coder_workspace" "me" {} +data "coder_workspace_owner" "me" {} + +resource "coder_agent" "main" { + arch = data.coder_provisioner.me.arch + os = "linux" + startup_script = <<-EOT + set -e + + # Prepare user home with default files on first start. + if [ ! -f ~/.init_done ]; then + cp -rT /etc/skel ~ + touch ~/.init_done + fi + + # Install the latest code-server. + # Append "--version x.x.x" to install a specific version of code-server. + curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server + + # Start code-server in the background. + /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 & + EOT + + # These environment variables allow you to make Git commits right away after creating a + # workspace. Note that they take precedence over configuration defined in ~/.gitconfig! + # You can remove this block if you'd prefer to configure Git manually or using + # dotfiles. (see docs/dotfiles.md) + env = { + 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}" + } + + # The following metadata blocks are optional. They are used to display + # information about your workspace in the dashboard. You can remove them + # if you don't want to display any information. + # For basic resources, you can use the `coder stat` command. + # If you need more control, you can write your own script. + metadata { + display_name = "CPU Usage" + key = "0_cpu_usage" + script = "coder stat cpu" + interval = 10 + timeout = 1 + } + + metadata { + display_name = "RAM Usage" + key = "1_ram_usage" + script = "coder stat mem" + interval = 10 + timeout = 1 + } + + metadata { + display_name = "Home Disk" + key = "3_home_disk" + script = "coder stat disk --path $${HOME}" + interval = 60 + timeout = 1 + } + + metadata { + display_name = "CPU Usage (Host)" + key = "4_cpu_usage_host" + script = "coder stat cpu --host" + interval = 10 + timeout = 1 + } + + metadata { + display_name = "Memory Usage (Host)" + key = "5_mem_usage_host" + script = "coder stat mem --host" + interval = 10 + timeout = 1 + } + + metadata { + display_name = "Load Average (Host)" + key = "6_load_host" + # get load avg scaled by number of cores + script = < Date: Thu, 17 Jul 2025 18:58:50 +0500 Subject: [PATCH 2/2] Update README.md --- registry/coder-labs/templates/docker-build/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/registry/coder-labs/templates/docker-build/README.md b/registry/coder-labs/templates/docker-build/README.md index 4c05d72b..94d01b88 100644 --- a/registry/coder-labs/templates/docker-build/README.md +++ b/registry/coder-labs/templates/docker-build/README.md @@ -2,9 +2,8 @@ display_name: Docker Build description: Build Docker containers from Dockerfile as Coder workspaces icon: ../../../../.icons/docker.svg -maintainer_github: coder verified: true -tags: [docker, container, build, dockerfile] +tags: [docker, container, dockerfile] --- # Remote Development on Docker Containers (Build from Dockerfile) @@ -42,7 +41,7 @@ This template provisions the following resources: This means, when the workspace restarts, any tools or files outside of the home directory are not persisted. To pre-bake tools into the workspace (e.g. `python3`), modify the `build/Dockerfile`. Alternatively, individual developers can [personalize](https://coder.com/docs/dotfiles) their workspaces with dotfiles. -> **Note** +> [!NOTE] > This template is designed to be a starting point! Edit the Terraform and Dockerfile to extend the template to support your use case. ### Editing the image