Since vscode came into being, I've moved from pycharm. Tired of the clumsiness of pycharm, I can only say that it's really fragrant after using vscode. It's the flash in the editor world. But like flash, it's light, but basically everything needs to be configured by yourself, all kinds of plug-ins and extensions. Some of pycharm's features may come with it, but vscode requires its own kinds of twists and turns, such as the topic of this article: local connection to containers on remote servers.
Requirements first: use VSCODE to connect the container on the remote server (such as Linux) locally (such as Windows), and you can modify the code in the editor.
After communicating with colleagues, we found that the way to realize this requirement is nothing more than to map port 22 of the container and log in Using ssh, which is equivalent to treating the container as a virtual remote server. Benefit from Remote - SSH , which is well implemented in VSCODE:
[remote server] start the container. Note that port 22 of the container needs to be mapped out, for example, to port 5222 of host: - p 5222:22.
[remote server container] set the root account password:
Follow the prompts to set the password. If you use the - u parameter to specify a non root user when you start the container, set the password of the account accordingly.
Many images in remote server container will not install ssh by default, so you need to install ssh service in the container:
apt update && apt install -y --no-install-recommends openssh-server
[remote server container] generally, the root account is used when entering the container, but ssh does not allow the root account to use password for remote login by default, so you need to modify the ssh configuration file to allow:
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
But if you use the - u parameter to specify a non root user when you start the container, you can skip this step.
[remote server container] start the ssh service:
service ssh start
[local] use VSCODE connection. When adding a new ssh host, the address is as follows:
ssh root@your-server-ip -A -p 5222
This 5222 is the mapping port set when starting the container in step 1. Then enter the password, just like the normal connection to the remote server.
In addition, before step 5, you can now use ssh to verify whether your local terminal can connect. If the following warning occurs: remote host identification has changed! Error:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is 39:e5:ef:60:f1:ae:08:b9:46:36:7c:7d:61:e8:c9:41. Please contact your system administrator. Add correct host key in /home/ataliba/.ssh/known_hosts to get rid of this message. Offending RSA key in /home/ataliba/.ssh/known_hosts:17 RSA host key for host.host.com.br has changed and you have requested strict checking. Host key verification failed.
The following command can be executed locally to regenerate the key:
ssh-keygen -R [your-server-ip]:port
Where port is the port mapped in step 1. for example
ssh-keygen -R [192.168.102.1]:5222
In general, the commands executed in the container are as follows:
# Set password passwd root # Replace domestic source sed -i s:/archive.ubuntu.com:/mirrors.aliyun.com:g /etc/apt/sources.list sed -i s:/security.ubuntu.com:/mirrors.aliyun.com:g /etc/apt/sources.list apt update && apt install -y --no-install-recommends openssh-server sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config service ssh start
So far, the main body of this paper is completed. But there should be another official way to solve my needs: Remote - Containers. I just can't figure it out after a long time. It's not as simple and fast as this one.
If you want to see my Tucao, you can make complaints about it, otherwise you can close the page.
Of course, the first thing I want to think about is to use Microsoft's official plug-in: Remote - Containers , this belongs to Remote Development Suite One of, mainly used to connect containers. The other two are as follows:
- Remote - SSH : connect to remote server, easy to use, daily use
- Remote - WSL : connected to the Linux subsystem of Windows, not used very much
Although it is one of the Remote Development kits, can it really solve my needs? It depends on how the Remote is defined.
By default, this Remote refers to the local container. Yes, it's local. It was after a lot of trouble that I realized. First, let's see how the official document defines this plug-in:
The Visual Studio Code Remote - Containers extension lets you use a Docker container as a full-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code's full feature set.
There is no mention of Remote in it. When it is mentioned, it is Advanced Container Configuration The Developing inside a container on a remote Docker host section in this document. In this introduction, I clearly realize that the default of Remote - Containers is to connect local containers (after all, compared with local containers, they are also remote operating systems):
Sometimes you may want to use the Remote - Containers extension to develop inside a container that sits on a remote server. Docker does not support mounting (binding) your local filesystem into a remote container, so VS Code's default devcontainer.json behavior to use your local source code will not work. While this is the default behavior, in this section we will cover connecting to a remote host so that you can either attach to any running container, or use a local devcontainer.json file as a way to configure, create, and connect to a remote dev container.
Look at the first sentence: Sometimes you may want to use the remote - containers extension to develop inside a container that bits on a remote server, Really? Is it Really sometime? Such frequent operations did not expect to be classified as "abnormal needs".
However, after installing dockers for Windows, restarting virtualization twice, etc., I gave up after a half day's tossing. You also need to install docker locally. It's really not as easy to use as my colleagues say pycharm s are.