VS Code is based on the development environment configuration of docker in the server

Based on the Dev Containers plugin

Of course, you can directly download the Dev Containers plug-in in vscode. After connecting to the server, find the corresponding docker connection in Dev Containers and develop directly.


At this point, you will see all the running docker containers on your server (you must ensure that the docker container you want to develop is running at this time), and then click the corresponding docker container to connect, and the subsequent operations will be It is consistent with remote-ssh connection server operation.

Based on Jump Machine

The springboard machine can be understood as using the server as a springboard, and then connecting to the docker container in the server through this springboard.

The server connects to the docker container through ssh

Note that the server can connect to the docker container directly docker exec -it <docker_container> /bin/bash, here is a demonstration of using ssh to connect to the docker container

ssh -p <port> [email protected]
# 注意此处的<port>是指docker容器22端口映射到本地的端口
# 即在创建docker容器的时候 docker run -it -p <host_port>:22 ......
# 此处的<host_port>就是指上述命令中的<port>

You can connect to the docker container through the above command, but it should be noted here that some parameters need to be modified in the docker container before the connection.

vim /etc/ssh/sshd_config

PubkeyAuthentication yes # 启用公钥私钥配对认证方式
PermitRootLogin yes # root能使用ssh登录
port=22 # 开启22端口

Then add the public key in the server ~/.ssh/id_rsa.pubto the file in the docker container ~/.ssh/authorized_keysto realize secret-free login.

VS Code configures the ssh config file to connect to the docker container

~/.ssh/configTake macOS as an example here, first find the files in the home directory

# 配置服务器作为跳板机
Host server  # 自定义即可
	User <username>
	HostName <server_ip>
	Port <port>  # 此处<port>是指服务器的ssh端口(默认是22)
	IdentityFile ~/ssh/id_rsa  # 如果是私钥连接服务器,则需要本地私钥文件

# 利用跳板机连接docker容器
Host server_docker  # 自定义即可
	User root
	HostName 127.0.0.1
	Port <map_port>  # docker容器22端口映射到server服务器的端口
	ProxyCommand ssh -W %h:%p server  # 此处的server是跳板机的名字 必须和跳板机的Host后面的名称一致

At this point, you will see the configured server and server_docker under the remote-ssh plug-in in VS Code, just click the docker container you want to connect to.

From the above operation steps, in fact, the two ssh connection methods have the same principle, that is, connect to the server first and then connect to the docker container. Now you can happily develop docker containers with VS Code!!!

Guess you like

Origin blog.csdn.net/qq_41139677/article/details/131361736