VSC - VS Code 远程开发环境
Posted anliven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VSC - VS Code 远程开发环境相关的知识,希望对你有一定的参考价值。
1 - VS Code Remote Development
1.1 Documentation
- Remote - https://code.visualstudio.com/docs/remote/
- Remote tutorials - https://code.visualstudio.com/docs/remote/remote-tutorials
- FAQ - https://code.visualstudio.com/docs/remote/faq
1.2 Remote Development extension pack
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
- Remote - SSH: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh
- Remote - Containers: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
- Remote - WSL:https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl
1.3 Comparison
Remote - SSH
Remote - Containers
Remote - WSL
2 - Remote - SSH
Documentation: https://code.visualstudio.com/docs/remote/ssh
2.1 Preparation
Extension
SSH
- 远程主机已安装ssh-server
- 本地主机已安装ssh-client
2.2 Configuration
点击界面左下角图标,弹出远程开发命令面板
Remote-SSH: Connect to Host... :
可以直接输入主机地址进行连接,相关配置不会添加到 SSH 配置文件中
也可以选择 Add New SSH Host...,输入完整的ssh指令(支持附加额外的 SSH 参数),相关配置会自动添加到 SSH 配置文件中
- Remote-SSH: Open Configuration File...:选取 SSH 配置文件并打开,自定义配置文件
- Remote-SSH: Getting Started...:会打开Getting Started 页面 https://code.visualstudio.com/docs/remote/ssh#_getting-started
2.3 Sample - Remote-SSH: Connect to Host
直接输入SSH信息并执行连接
选择目标主机的系统类型
输入密码
连接成功
查看信息
在目标主机可以查看到已经自动部署了 Remote Server,路径为 ~/.vscode-server/
[anliven@anliven ~]$ ls -l .vscode-server/
total 0
drwxrwxr-x 3 anliven anliven 54 Jun 24 03:58 bin
drwxrwxr-x 5 anliven anliven 62 Jun 24 04:02 data
drwxrwxr-x 3 anliven anliven 40 Jun 24 04:03 extensions
[anliven@anliven ~]$
[anliven@anliven ~]$ tree -L 3 .vscode-server/
.vscode-server/
├── bin
│ └── ff915844119ce9485abfe8aa9076ec76b5300ddd
│ ├── bin
│ ├── extensions
│ ├── LICENSE
│ ├── node
│ ├── node_modules
│ ├── out
│ ├── package.json
│ ├── product.json
│ └── server.sh
├── data
│ ├── logs
│ │ ├── 20200624T040232
│ │ └── 20200624T041953
│ ├── Machine
│ ├── machineid
│ └── User
│ └── globalStorage
└── extensions
└── hediet.vscode-drawio-0.6.6
├── CHANGELOG.md
├── dist
├── docs
├── drawio
├── package.json
└── README.md
18 directories, 9 files
[anliven@anliven ~]$
2.4 Sample - Remote-SSH: Open Configuration File
2.4.1 Add and configure
输入连接信息
选择将连接信息存放在哪个配置文件
提示已添加主机
选择主机,点击鼠标右键选择连接方式
连接成功
2.4.2 Configuration change
- 在命令面板中,Remote-SSH: Open Configuration File...:选取 SSH 配置文件并打开,自定义配置文件
- 通过远程资源连接器
可以查看和修改相应的配置信息,例如,添加端口信息 Port 22,指定秘钥文件 IdentityFile~/.ssh/id_rsa.pub
2.5 Login without password
将本地生成的公钥添加到目标主机的 ~/.ssh/authorized_keys
, 此后登录就不用输密码。
GuowangLi@CHN-L-GuowangLi /c/Projects
λ ls -l ~/.ssh/id_rsa.pub
-rw-r--r-- 1 GuowangLi 1049089 579 Apr 27 18:22 /c/Users/guowangli/.ssh/id_rsa.pub
GuowangLi@CHN-L-GuowangLi /c/Projects
λ ssh-copy-id root@192.168.56.200
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/guowangli/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.200‘s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh ‘root@192.168.56.200‘"
and check to make sure that only the key(s) you wanted were added.
GuowangLi@CHN-L-GuowangLi /c/Projects
λ
2.6 Extension
给远程环境安装插件,对本机的VS Code没有影响。
插件在远端提供功能,比如代码审查、自动补齐等等,而这所有的一切就像在本地操作一样,对文件的更改也是直接操作的远程环境中的文件。
Note:需要以非root用户来进行Remote SSH连接。否则无法在SSH远程环境无安装扩展
3 - Remote - Containers
Documentation: https://code.visualstudio.com/docs/remote/containers
3.1 Preparation
System Requirements
- Linux: Docker CE/EE 18.06
- Docker Compose 1.21+
[root@anliven ~]# uname -a
Linux anliven 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@anliven ~]#
[root@anliven ~]# cat /etc/system-release
CentOS Linux release 7.8.2003 (Core)
[root@anliven ~]#
[root@anliven ~]# docker version
Client: Docker Engine - Community
Version: 19.03.11
API version: 1.40
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:13:48 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.11
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 42e35e61f3
Built: Mon Jun 1 09:12:26 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
[root@anliven ~]#
[root@anliven ~]# docker-compose --version
docker-compose version 1.26.0, build d4451659
[root@anliven ~]#
VS Code
Installation - Linux https://code.visualstudio.com/docs/setup/linux
[root@anliven ~]# su - anliven
Last login: Mon Jun 15 20:24:01 EDT 2020 on :0
[anliven@anliven ~]$
[anliven@anliven ~]$ code --version
1.46.0
a5d1cc28bb5da32ec67e86cc50f84c67cc690321
x64
[anliven@anliven ~]$
注意: 应该以非super用户启动code
[root@anliven ~]# code --version
You are trying to start vscode as a super user which is not recommended. If you really want to, you must specify an alternate user data directory using the --user-data-dir argument.
[root@anliven ~]#
3.2 Try a Sample
New Container
Sample - Java
https://github.com/microsoft/vscode-remote-try-java
开始创建
创建完成
镜像与容器
[root@anliven ~]# docker images vsc*
REPOSITORY TAG IMAGE ID CREATED SIZE
vsc-vscode-remote-try-java-7082d7a76a508a754975227521a1e638 latest 886f9cad72dc 3 minutes ago 554MB
vsc-vscode-remote-try-python-41cc2ee6f8cb6b0cee4c350f9a78ef53 latest 37ccde86ffac 33 minutes ago 1.15GB
vsc-volume-bootstrap latest f359f0df1483 36 minutes ago 130MB
[root@anliven ~]#
[root@anliven ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f360dd02acba vsc-vscode-remote-try-java-7082d7a76a508a754975227521a1e638 "/bin/sh -c ‘echo Co…" 3 minutes ago Up 3 minutes wizardly_benz
[root@anliven ~]#
Close the Remote Connection
打开远程窗口, 选择“关闭远程连接”, 对应的容器也将停止运行。
Some operations
可以通过remote-container操作已经存在的容器
3.3 Remote-Containers: Open Folder in Container
目录信息
此时目录为空目录,没有对应的配置,将会触发“新建配置”的过程
[anliven@anliven samples]$ pwd
/home/anliven/samples
[anliven@anliven samples]$ ls -la
total 4
drwxrwxr-x 2 anliven anliven 6 Jun 18 03:19 .
drwx------. 20 anliven anliven 4096 Jun 18 03:19 ..
[anliven@anliven samples]$
选择目录
选择类型
开始创建
根据配置信息,下载基础镜像、构建新镜像, 然后启动容器
创建完成
生成的目录与文件
[anliven@anliven samples]$ ls -la
total 4
drwxrwxr-x 3 anliven anliven 27 Jun 18 03:46 .
drwx------. 20 anliven anliven 4096 Jun 18 03:19 ..
drwxrwxr-x 2 anliven anliven 49 Jun 18 03:46 .devcontainer
[anliven@anliven samples]$
[anliven@anliven samples]$ ls -la .devcontainer/
total 8
drwxrwxr-x 2 anliven anliven 49 Jun 18 03:46 .
drwxrwxr-x 3 anliven anliven 27 Jun 18 03:46 ..
-rw-rw-r-- 1 anliven anliven 1398 Jun 18 03:46 devcontainer.json
-rw-rw-r-- 1 anliven anliven 997 Jun 18 03:46 Dockerfile
[anliven@anliven samples]$
[anliven@anliven samples]$ cat .devcontainer/Dockerfile |grep -v "//"
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
#-------------------------------------------------------------------------------------------------------------
# Update the VARIANT arg in devcontainer.json to pick an Ubuntu version: 20.04, 18.04
# To fully customize the contents of this image, use the following Dockerfile instead:
ARG VARIANT="20.04"
FROM mcr.microsoft.com/vscode/devcontainers/base:0-ubuntu${VARIANT}
# ** [Optional] Uncomment this section to install additional packages. **
#
# RUN apt-get update # && export DEBIAN_FRONTEND=noninteractive # && apt-get -y install --no-install-recommends <your-package-list-here>
[anliven@anliven samples]$
[anliven@anliven samples]$ cat .devcontainer/Dockerfile |grep -v "#"
ARG VARIANT="20.04"
FROM mcr.microsoft.com/vscode/devcontainers/base:0-ubuntu${VARIANT}
相关的镜像与容器
[anliven@anliven samples]$ docker images mcr.microsoft.com/vscode/devcontainers/base
REPOSITORY TAG IMAGE ID CREATED SIZE
mcr.microsoft.com/vscode/devcontainers/base 0-ubuntu20.04 2b5fb7ba7ef2 23 hours ago 295MB
[anliven@anliven samples]$
[anliven@anliven samples]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10dfd5f1a69e vsc-samples-2ceaaed4d0d4ec6e208545a84baaada7 "/bin/sh -c ‘echo Co…" 11 minutes ago Up 11 minutes vigilant_rubin
[anliven@anliven samples]$
关闭远程连接
关闭远程连接后,对应的容器也停止运行
[anliven@anliven samples]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[anliven@anliven samples]$
[anliven@anliven samples]$ docker ps -a | grep samples
10dfd5f1a69e vsc-samples-2ceaaed4d0d4ec6e208545a84baaada7 "/bin/sh -c ‘echo Co…" 14 minutes ago Exited (137) 44 seconds ago vigilant_rubin
[anliven@anliven samples]$
重新打开(只是重启了已存在的容器)
启动了现有的容器
[anliven@anliven samples]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10dfd5f1a69e vsc-samples-2ceaaed4d0d4ec6e208545a84baaada7 "/bin/sh -c ‘echo Co…" 20 minutes ago Up 3 minutes vigilant_rubin
3.4 Remote-Containers: Attach to Running Container
如果有正在运行的容器
[anliven@anliven samples]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10dfd5f1a69e vsc-samples-2ceaaed4d0d4ec6e208545a84baaada7 "/bin/sh -c ‘echo Co…" 45 minutes ago Up 6 minutes vigilant_rubin
6a5c0837e615 test-node-web-app "docker-entrypoint.s…" 47 hours ago Up 4 seconds 0.0.0.0:8080->8080/tcp objective_rhodes
[anliven@anliven samples]$
打开远程窗口后,将出现“Remote-Containers: Attach to Running Container” 的选项
选择容器
将会在对应的容器启动远程环境, 终端也将变成容器中的终端
3.5 Some configureations
官方示例中,涉及 .devcontainer.json 和 Dockerfile 两个文件
- .devcontainer : 开发环境配置目录
- devcontainer.json : 环境配置文件,定义容器运行时的配置
- Dockerfile : 环境的Docker镜像生成文件
- .vscode : vscode使用的配置文件(容器端使用)
- launch.json : debuger 配置文件(容器端使用)
devcontainer.json reference: https://code.visualstudio.com/docs/remote/containers#_devcontainerjson-reference
Dockerfile reference: https://docs.docker.com/engine/reference/builder/
4 - Remote - WSL
Documentation: https://code.visualstudio.com/docs/remote/wsl
适用于 Linux 的 Windows 子系统文档
5 - Troubleshooting
TS-1
Description
Solution
https://docs.docker.com/engine/install/linux-postinstall/
To create the docker group and add your user:
Create the docker group.
$ sudo groupadd docker
Add your user to the docker group.
$ sudo usermod -aG docker $USER
Log out and log back in so that your group membership is re-evaluated.
If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.
On Linux, you can also run the following command to activate the changes to groups:
$ newgrp docker
Verify that you can run docker commands without sudo.
$ docker run hello-world
TS-2
Description
Solution
为普通用户添加sudo命令的权限
# 1.切换到root用户,添加sudo文件的写权限
chmod u+w /etc/sudoers
# 2.编辑sudoers文件,在 root ALL=(ALL) ALL 一行下面添加 <user-name> ALL=(ALL) ALL (允许<user-name>执行sudo命令, 但需要输入密码)
user ALL=(ALL) ALL # 允许用户user执行sudo命令(需要输入密码)
%usergroup ALL=(ALL) ALL # 允许用户组usergroup里的用户执行sudo命令(需要输入密码)
user ALL=(ALL) NOPASSWD: ALL # 允许用户user执行sudo命令,并且不输入密码
%usergroup ALL=(ALL) NOPASSWD: ALL # 允许用户组usergroup里的用户执行sudo命令,并且不输入密码
# 3.撤销sudoers文件写权限
chmod u-w /etc/sudoers
TS-3
Description
VS code的远程环境提示“找不到扩展”
Solution
需要以非root用户来进行Remote SSH连接。
以上是关于VSC - VS Code 远程开发环境的主要内容,如果未能解决你的问题,请参考以下文章
VS Code 安装与使用,Python 顶级 IDE 编程指南!