https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpackjava
Remote - SSH
Remote - Containers
Remote - WSL
node
Documentation: https://code.visualstudio.com/docs/remote/sshpython
Extension
SSHlinux
点击界面左下角图标,弹出远程开发命令面板
Remote-SSH: Connect to Host... :
能够直接输入主机地址进行链接,相关配置不会添加到 SSH 配置文件中
也能够选择 Add New SSH Host...,输入完整的ssh指令(支持附加额外的 SSH 参数),相关配置会自动添加到 SSH 配置文件中
git
直接输入SSH信息并执行链接
选择目标主机的系统类型
输入密码
链接成功
查看信息
在目标主机能够查看到已经自动部署了 Remote Server,路径为 ~/.vscode-server/
github
[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 ~]$
输入链接信息
选择将链接信息存放在哪一个配置文件
提示已添加主机
选择主机,点击鼠标右键选择链接方式
链接成功
web
~/.ssh/id_rsa.pub
将本地生成的公钥添加到目标主机的 ~/.ssh/authorized_keys
, 此后登陆就不用输密码。docker
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 λ
给远程环境安装插件,对本机的VS Code没有影响。
插件在远端提供功能,好比代码审查、自动补齐等等,而这全部的一切就像在本地操做同样,对文件的更改也是直接操做的远程环境中的文件。
Note:须要以非root用户来进行Remote SSH链接。不然没法在SSH远程环境无安装扩展json
Documentation: https://code.visualstudio.com/docs/remote/containersbootstrap
System Requirements
[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 ~]#
New Container
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 ~]#
打开远程窗口, 选择“关闭远程链接”, 对应的容器也将中止运行。
能够经过remote-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
若是有正在运行的容器
[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” 的选项
选择容器
将会在对应的容器启动远程环境, 终端也将变成容器中的终端
官方示例中,涉及 .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/
Documentation: https://code.visualstudio.com/docs/remote/wsl
适用于 Linux 的 Windows 子系统文档
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
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
Description
VS code的远程环境提示“找不到扩展”
Solution 须要以非root用户来进行Remote SSH链接。