From 4ba42a91243c19e461b5456c4a4ed5e7dc352d87 Mon Sep 17 00:00:00 2001 From: i-play-lf2 <914637118@qq.com> Date: Tue, 6 Jan 2026 07:17:11 +0000 Subject: [PATCH 1/4] refactor: rework devcontainer template layout --- .devcontainer/devcontainer.json | 17 ----------------- .gitignore | 3 ++- README.md | 14 +++++++------- compose-base/docker-compose.yaml | 8 ++++++++ .../.template/.devcontainer/devcontainer.json | 12 ++++++++++++ projects/.template/.env | 5 +++++ projects/.template/docker-compose.yaml | 10 ++++++++++ 7 files changed, 44 insertions(+), 25 deletions(-) delete mode 100644 .devcontainer/devcontainer.json create mode 100644 compose-base/docker-compose.yaml create mode 100644 projects/.template/.devcontainer/devcontainer.json create mode 100644 projects/.template/.env create mode 100644 projects/.template/docker-compose.yaml diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json deleted file mode 100644 index b3809f9..0000000 --- a/.devcontainer/devcontainer.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "build": { - "args": { - "SDK_VERSION": "${localEnv:SDK_VERSION}" - }, - "dockerfile": "../${localEnv:DOCKER_FILE_PATH}" - }, - "initializeCommand": "docker volume create ${localEnv:VSCODE_CONTAINER_PROJECT}", - "name": "${localEnv:VSCODE_CONTAINER_PROJECT}", - "postCreateCommand": "sudo chown vscode ${containerWorkspaceFolder}", - "runArgs": [ - "--name", - "${localEnv:VSCODE_CONTAINER_PROJECT}" - ], - "workspaceFolder": "/mnt/${localEnv:VSCODE_CONTAINER_PROJECT}", - "workspaceMount": "source=${localEnv:VSCODE_CONTAINER_PROJECT},target=/mnt/${localEnv:VSCODE_CONTAINER_PROJECT},type=volume" -} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2eea525..541e1c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +/projects/* +!/projects/.template \ No newline at end of file diff --git a/README.md b/README.md index c67e4c4..7569a6c 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,19 @@ # VS Code Dev Container Playground -This repository keeps a minimal VS Code Dev Container setup that runs on top of Docker. With parameterized environment variables consumed by `.devcontainer/devcontainer.json`, the same workflow can prepare a baseline container environment for different development needs. +This repository provides a template-based workflow for VS Code Dev Containers. Shared settings live under `compose-base/`, and each local dev container is created by copying `projects/.template/` into `projects/`. ## Getting Started Prerequisites: Docker is available on the host, and VS Code has the `ms-vscode-remote.remote-containers` extension installed. 1. Download the repository (e.g., `git clone https://github.com/iplaylf2/vscode-container.git`). -2. Set the required environment variables (for example `VSCODE_CONTAINER_PROJECT`, `DOCKER_FILE_PATH`, `SDK_VERSION`). -3. Open the folder in VS Code and run `Dev Containers: Reopen in Container`. +2. Copy `projects/.template/` to `projects/`. +3. Edit `projects//docker-compose.yaml` as needed. +4. Set `VSCODE_CONTAINER_PROJECT` in the host environment that will launch VS Code. +5. Open `projects/` in VS Code and run `Dev Containers: Reopen in Container`. -After the container is created, VS Code reopens the workspace inside the container. When the volume is freshly created, that directory starts empty. +After the container is created, VS Code opens the workspace at `/mnt/${VSCODE_CONTAINER_PROJECT}`. The data lives in a Docker volume named `${VSCODE_CONTAINER_PROJECT}`. ## Configuration Notes -- `VSCODE_CONTAINER_PROJECT` controls both the Docker volume name and the `/mnt/` path inside the container. -- Keep `DOCKER_FILE_PATH` and `SDK_VERSION` as environment variables so switching Dockerfiles or toolchains only requires updating those values. -- The default container user is `vscode`; `postCreateCommand` ensures that account owns the workspace directory. +- `VSCODE_CONTAINER_PROJECT` provides the project identity (volume name and `/mnt/` path); it is an environment variable because `devcontainer.json` cannot pass variables into Docker Compose. diff --git a/compose-base/docker-compose.yaml b/compose-base/docker-compose.yaml new file mode 100644 index 0000000..3a564a7 --- /dev/null +++ b/compose-base/docker-compose.yaml @@ -0,0 +1,8 @@ +services: + devcontainer: + volumes: + - devcontainer-workspace:/mnt/${VSCODE_CONTAINER_PROJECT} + +volumes: + devcontainer-workspace: + name: ${VSCODE_CONTAINER_PROJECT} diff --git a/projects/.template/.devcontainer/devcontainer.json b/projects/.template/.devcontainer/devcontainer.json new file mode 100644 index 0000000..82e11f5 --- /dev/null +++ b/projects/.template/.devcontainer/devcontainer.json @@ -0,0 +1,12 @@ +{ + "name": "${localEnv:VSCODE_CONTAINER_PROJECT}", + "dockerComposeFile": [ + "../../../compose-base/docker-compose.yaml", + "../docker-compose.yaml" + ], + "service": "devcontainer", + "workspaceFolder": "/mnt/${localEnv:VSCODE_CONTAINER_PROJECT}", + "initializeCommand": "docker volume create ${localEnv:VSCODE_CONTAINER_PROJECT}", + "postCreateCommand": "sudo chown vscode ${containerWorkspaceFolder}", + "overrideCommand": true +} diff --git a/projects/.template/.env b/projects/.template/.env new file mode 100644 index 0000000..943712d --- /dev/null +++ b/projects/.template/.env @@ -0,0 +1,5 @@ +# Project environment variables (add more as needed). +# Prefer setting values in the host environment before launching VS Code. + +# Container name and workspace path. +# VSCODE_CONTAINER_PROJECT=your-project-name diff --git a/projects/.template/docker-compose.yaml b/projects/.template/docker-compose.yaml new file mode 100644 index 0000000..cef9e29 --- /dev/null +++ b/projects/.template/docker-compose.yaml @@ -0,0 +1,10 @@ +services: + devcontainer: + # Base settings come from ../../compose-base/docker-compose.yaml via dockerComposeFile. + build: + context: ../.. + # Pick the Dockerfile that matches your dev environment. + dockerfile: images/base/Dockerfile + args: + # Optional: toolchain version for Dockerfiles that accept SDK_VERSION. + SDK_VERSION: 0 From 4f0ff699a4774fbe2c42f99cb156a0660ed65b36 Mon Sep 17 00:00:00 2001 From: i-play-lf2 <914637118@qq.com> Date: Tue, 6 Jan 2026 08:09:18 +0000 Subject: [PATCH 2/4] fix --- compose-base/docker-compose.yaml | 2 ++ projects/.template/.env | 5 ----- projects/.template/docker-compose.yaml | 1 - 3 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 projects/.template/.env diff --git a/compose-base/docker-compose.yaml b/compose-base/docker-compose.yaml index 3a564a7..c0cdaf3 100644 --- a/compose-base/docker-compose.yaml +++ b/compose-base/docker-compose.yaml @@ -1,5 +1,7 @@ services: devcontainer: + build: + context: .. volumes: - devcontainer-workspace:/mnt/${VSCODE_CONTAINER_PROJECT} diff --git a/projects/.template/.env b/projects/.template/.env deleted file mode 100644 index 943712d..0000000 --- a/projects/.template/.env +++ /dev/null @@ -1,5 +0,0 @@ -# Project environment variables (add more as needed). -# Prefer setting values in the host environment before launching VS Code. - -# Container name and workspace path. -# VSCODE_CONTAINER_PROJECT=your-project-name diff --git a/projects/.template/docker-compose.yaml b/projects/.template/docker-compose.yaml index cef9e29..3d8f540 100644 --- a/projects/.template/docker-compose.yaml +++ b/projects/.template/docker-compose.yaml @@ -2,7 +2,6 @@ services: devcontainer: # Base settings come from ../../compose-base/docker-compose.yaml via dockerComposeFile. build: - context: ../.. # Pick the Dockerfile that matches your dev environment. dockerfile: images/base/Dockerfile args: From 37ee94655fbe9005c157978f9b6ee088790883a4 Mon Sep 17 00:00:00 2001 From: i-play-lf2 <914637118@qq.com> Date: Tue, 6 Jan 2026 08:23:14 +0000 Subject: [PATCH 3/4] fix --- compose-base/docker-compose.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compose-base/docker-compose.yaml b/compose-base/docker-compose.yaml index c0cdaf3..8b4d962 100644 --- a/compose-base/docker-compose.yaml +++ b/compose-base/docker-compose.yaml @@ -1,3 +1,5 @@ +name: ${VSCODE_CONTAINER_PROJECT} + services: devcontainer: build: From b67a01e51a6c50aac4d5bf1edfaffc7721177aa8 Mon Sep 17 00:00:00 2001 From: i-play-lf2 <914637118@qq.com> Date: Tue, 6 Jan 2026 08:48:19 +0000 Subject: [PATCH 4/4] remove initializeCommand --- projects/.template/.devcontainer/devcontainer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/projects/.template/.devcontainer/devcontainer.json b/projects/.template/.devcontainer/devcontainer.json index 82e11f5..fd933b5 100644 --- a/projects/.template/.devcontainer/devcontainer.json +++ b/projects/.template/.devcontainer/devcontainer.json @@ -6,7 +6,6 @@ ], "service": "devcontainer", "workspaceFolder": "/mnt/${localEnv:VSCODE_CONTAINER_PROJECT}", - "initializeCommand": "docker volume create ${localEnv:VSCODE_CONTAINER_PROJECT}", "postCreateCommand": "sudo chown vscode ${containerWorkspaceFolder}", "overrideCommand": true }