Tóm tắt: Remote-SSH (ms-vscode-remote.remote-ssh): VS Code đọc ~/.ssh/config, SSH vào host, tự cài VS Code Server vào ~/.vscode-server rồi mở thư mục từ xa; extension chạy phía server. Dev Containers (ms-vscode-remote.remote-containers): khai báo .devcontainer/devcontainer.json trong repo, VS Code build/chạy container và mở project bên trong, mọi người trong team có cùng môi trường. Hai cái kết hợp được: SSH vào server có Docker rồi mở Dev Container trên đó.
Luồng hoạt động
flowchart LR L["VS Code (laptop)"] -->|"SSH, theo ~/.ssh/config"| H["Host từ xa"] H --> S["VS Code Server (~/.vscode-server)"] S --> E["Extension, terminal, debugger"] L -->|"Reopen in Container"| D["Docker (local hoặc trên host)"] D --> C["Container từ devcontainer.json"] C --> E2["Extension, uv, ruff trong container"]
Cấu hình
Remote-SSH
Khai báo host trong ~/.ssh/config trước, VS Code dùng lại nguyên cấu hình đó (kể cả ProxyJump qua bastion, xem SSH config và jump host):
Host lab-jump
HostName jump.example.com
User netops
IdentityFile ~/.ssh/id_ed25519
Host lab-dev
HostName 198.51.100.20
User netops
ProxyJump lab-jump| Thao tác | Cách làm |
|---|---|
| Kết nối | F1 → Remote-SSH: Connect to Host... → chọn lab-dev |
| Mở thẳng thư mục từ terminal | code --remote ssh-remote+lab-dev /home/netops/netinv |
| Mở file cấu hình SSH | F1 → Remote-SSH: Open SSH Configuration File... |
| Xem log khi kết nối lỗi | Panel Output → chọn Remote - SSH |
| Cài lại server bị hỏng | F1 → Remote-SSH: Kill VS Code Server on Host... rồi kết nối lại |
| Forward port app trên host về laptop | Tab Ports → Forward a Port → 8000 |
| Cài sẵn extension mỗi lần vào host mới | Setting remote.SSH.defaultExtensions |
{
"remote.SSH.remotePlatform": { "lab-dev": "linux" },
"remote.SSH.connectTimeout": 30,
"remote.SSH.defaultExtensions": ["ms-python.python", "charliermarsh.ruff"]
}VS Code Server chạy với quyền user SSH và extension có thể chạy code trên host. Không mở Remote-SSH bằng tài khoản có quyền cao trên máy production; dùng jump host/lab riêng.
Dev Containers
{
"name": "netinv",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {}
},
"containerEnv": { "UV_LINK_MODE": "copy" },
"remoteEnv": { "NET_USER": "${localEnv:NET_USER}" },
"mounts": [
"source=${localEnv:HOME}/.ssh,target=/home/vscode/.ssh,type=bind,readonly"
],
"forwardPorts": [8000],
"postCreateCommand": "pipx install uv && uv sync",
"customizations": {
"vscode": {
"extensions": ["ms-python.python", "charliermarsh.ruff", "redhat.vscode-yaml"],
"settings": { "python.defaultInterpreterPath": "${containerWorkspaceFolder}/.venv/bin/python" }
}
},
"remoteUser": "vscode"
}| Khoá | Ý nghĩa |
|---|---|
image / build.dockerfile |
Dùng image có sẵn, hoặc build từ Dockerfile trong .devcontainer/ |
features |
Gói cài thêm dạng plug-in (Docker CLI, Node, kubectl...), tham số trong {} |
containerEnv / remoteEnv |
Env cho cả container / chỉ cho process VS Code mở (terminal, debugger); ${localEnv:VAR} lấy từ máy laptop |
mounts |
Bind mount thêm (SSH key, cache) |
forwardPorts |
Port tự forward về laptop |
postCreateCommand |
Chạy một lần sau khi tạo container: cài dependency |
customizations.vscode |
Extension và setting chỉ áp trong container |
remoteUser |
User VS Code dùng trong container (image devcontainers/* có sẵn vscode) |
| Thao tác | Cách làm |
|---|---|
| Mở project trong container | F1 → Dev Containers: Reopen in Container |
Build lại sau khi sửa devcontainer.json |
Dev Containers: Rebuild Container |
| Quay về máy local | Dev Containers: Reopen Folder Locally |
| Tạo file mẫu | Dev Containers: Add Dev Container Configuration Files... |
| Dev Container trên server từ xa | Remote-SSH vào server có Docker → mở thư mục → Reopen in Container |
Mount ~/.ssh dạng readonly như trên để Ansible/Netmiko trong container SSH được tới thiết bị lab. Nếu dùng ssh-agent thì không cần mount: Dev Containers tự chuyển tiếp agent của máy local vào container.
Lỗi thường gặp
| Triệu chứng | Nguyên nhân | Cách xử lý |
|---|---|---|
| Kẹt ở "Setting up SSH Host... Downloading VS Code Server" | Host không ra được Internet | Setting "remote.SSH.localServerDownload": "always" để laptop tải rồi copy lên |
| Kết nối được SSH thường nhưng Remote-SSH báo lỗi tunnel/port forwarding | sshd_config có AllowTcpForwarding no |
Bật AllowTcpForwarding yes cho user đó (hỏi admin host) |
| Báo host không đáp ứng yêu cầu (glibc, libstdc++) | Distro quá cũ (vd CentOS 7) | Dùng host mới hơn, hoặc chạy Dev Container trên host đó |
| Hỏi mật khẩu liên tục, chậm | Không dùng key/agent, mỗi kết nối đăng nhập lại | Dùng SSH key + agent; ControlMaster trong ~/.ssh/config |
| Server cũ chạy hỏng sau khi update VS Code | Bản server không khớp hoặc file hỏng | Kill VS Code Server on Host, hoặc xoá ~/.vscode-server trên host |
postCreateCommand lỗi, container vẫn mở nhưng thiếu gói |
Lệnh fail (mạng, quyền) | Xem log Dev Containers, sửa rồi Rebuild Container |
| File tạo trong container thuộc root trên host | Container chạy root | Đặt remoteUser non-root (vscode) |
Sửa devcontainer.json không có tác dụng |
Container cũ vẫn chạy | Rebuild Container (thêm Without Cache nếu đổi image) |