簡體   English   中英

VSCode docker 開發容器無法訪問~/.ssh

[英]VSCode docker dev container can't access ~/.ssh

我需要訪問/home/vscode/.ssh/以便我使用的工具可以使用我的 ssh 密鑰,但是我似乎無法更改權限。

我像這樣構建容器:

# other steps

COPY id_rsa /home/vscode/.ssh/id_rsa
RUN chmod 600 /home/vscode/.ssh/id_rsa \
  && touch /home/vscode/.ssh/known_hosts \
  && ssh-keyscan bitbucket.org >> /home/vscode/.ssh/known_hosts

# other steps 

這會從我的本地目錄中獲取我的id_rsa ,並將其添加到 docker 容器中,這樣我就可以繼續使用現有的 SSH 密鑰。

然后我嘗試使用像 terraform 這樣的工具來執行一個命令,該命令從使用我的 SSH 密鑰設置的存儲庫中克隆一些代碼。

$ terraform plan

ERRO[0001] 1 error occurred:
        * error downloading 'ssh://git@bitbucket.org/example/example.git?ref=master': /usr/bin/git exited with 128: Cloning into '/workspaces/example'...
Failed to add the RSA host key for IP address 'xxx.xxx.xxx.xxx' to the list of known hosts (/home/vscode/.ssh/known_hosts).
Load key "/home/vscode/.ssh/id_rsa": Permission denied
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.

如何在我的 Dockerfile 中正確設置訪問權限? 我什至無法運行ssh-kegyen

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vscode/.ssh/id_rsa): 
/home/vscode/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Saving key "/home/vscode/.ssh/id_rsa" failed: Permission denied

在多次嘗試覆蓋 Dockerfile 中的用戶權限后,我發現用戶是從devcontainer.json文件中設置的。

只需將 go 移至設置“remoteUser”的底部"remoteUser": "vscode"並將其注釋掉。

這現在會將用戶設置為root 對於本地開發,這是完美的,應該不會造成任何問題。 我現在可以使用 SSH 密鑰以及之前鎖定的任何其他密鑰。

// .devcontainer/devcontainer.json

{
    "name": "Ubuntu",
    "build": {
        "dockerfile": "Dockerfile",
        // Update 'VARIANT' to pick an Ubuntu version: focal, bionic
        "args": { "VARIANT": "focal" }
    },

    "settings": {},

    "extensions": [],

    // "remoteUser": "vscode"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM