[英]Can I develop with VS Code in containers on a remote host running Windows/WSL2?
[英]SSH forwarding using WSL2 and VS code containers on Windows
我有 Ubuntu 在 Windows 上的 WSL2 下運行。 在 Ubuntu 內部,我已經克隆了我的存儲庫,該存儲庫設置為運行 docker。 當我在項目中運行docker-compose up
,它成功啟動,我可以從 Windows 上的 VS 代碼打開容器。
當我嘗試從 VS 代碼內部使用任何 git 功能時,就會出現問題。 我只是得到一個permission denied (publickey)
。 如果我在 VS 代碼(連接到容器)中打開終端,在運行git pull
時會出現同樣的錯誤。
If I run docker-compose run web bash
from the Ubuntu terminal, I can successfully run git pull
. 所以代理被轉發到容器,它只是在 VS Code 中不起作用。
我缺少一些設置嗎?
To get VS Code to forward your SSH keys from your WSL2 instance into a Docker conatiner running on the WSL2 backend, you need to tell WSL2 to create an ssh-agent at startup , and add your ssh key to the agent . 當 VS Code 附加到在 WSL2 后端運行的容器時,它會自動獲取正在運行的 ssh-agent,並允許您使用容器內的 WSL2 SSH 密鑰進行身份驗證。
對於任何一種方法,您都需要在 WSL2 中安裝socat
sudo apt install socat
要告訴您的 WSL2 發行版在啟動時啟動它的 ssh-agent,您需要將這些行添加到您的 ~/.bash_profile 或 ~/.zprofile(對於 Zsh),以便 ssh-agent 在 WSL2 啟動時啟動:
if [ -z "$SSH_AUTH_SOCK" ]; then
# Check for a currently running instance of the agent
RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
if [ "$RUNNING_AGENT" = "0" ]; then
# Launch a new instance of the agent
ssh-agent -s &> $HOME/.ssh/ssh-agent
fi
eval `cat $HOME/.ssh/ssh-agent`
fi
# also add your key to this ssh-agent session
#
# When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa,
# ~/.ssh/id_ecdsa, ~/.ssh/id_ecdsa_sk, ~/.ssh/id_ed25519, and ~/.ssh/id_ed25519_sk.
# Source: https://man.cx/ssh-add
ssh-add
# if you had to create ~/.bash_profile, these lines may also be needed
# to load your ~/.bashrc config
#
# test and run the .bashrc file if it exists (this is the default on Ubuntu for WSL2)
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
鑰匙串基本上和上面一樣,但是用一個簡單的命令。 默認情況下未安裝它,因此您必須使用發行版的 package 管理器或從源代碼安裝它。
sudo apt install keychain
安裝鑰匙串后,將以下內容添加到您的~/.bash_profile
或~/.zprofile
:
# keychain will start the ssh agent and add the keys, or reuse the ssh agent
# if it is already running
eval `keychain --eval --agents ssh id_rsa`
# ...
您還可以使用鑰匙串來設置您的 GPG 密鑰(如果有)。
如果~/.bash profile
不存在,(它不適用於 WSL2 上 Ubuntu 的默認安裝),那么您需要將以下行添加到~/.bash_profile
的末尾,以便~/.bashrc
使用 bash 時來源正確。
# run the .bashrc file if it exists (this is the default on WSL2 if this does not already exist)
# these lines may already exist if .bash_profile already exists
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
https://code.visualstudio.com/docs/remote/containers#_using-ssh-keys
https://github.com/microsoft/vscode-remote-release/issues/2925#issuecomment-652558889
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.