Programming

[VSCode] Windows에서 remote-container vscode 붙이기

stein 2022. 1. 20. 11:55

TL;DR

https://code.visualstudio.com/remote/advancedcontainers/develop-remote-host

 

Develop a container on a remote Docker host

Develop a container on a remote Docker host

code.visualstudio.com

본 게시글은 위 과정을 따릅니다. 

 

VSCode 그리고 Docker

vscode는 정말 엄청난 속도로 발전하고 있다. 대부분의 프로젝트에서 vscode로 처리할 수 있으니 말이다. 그 중에서도 docker와 관련된 'Fancy'한 기능인, vscode를 remote-container에 연결하는 방법을 한 번 알아보자.

 

remote container? attach vscode?

remote container

외부 서버에 띄운 container를 remote container라고 일컫는다.

 

attach vscode

attach vscode는 container 내부에 cli(terminal)로 접근하는 것이 아닌, vscode가 직접 들어가서 마치 local처럼 작업하는 기능이다. (vscode remote ssh와 비슷한 느낌이다)

extensions

WSL을 제외하고 모든 extensions를 받아주자.

 

windows 설정

https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse

 

Get started with OpenSSH

Installing OpenSSH Client and Server for Windows.

docs.microsoft.com

openSSH를 설치해준다. windows 설정에서 optional features를 검색하면 진입할 수 있다.

client, server 모두 설치한다.

권한 설정

1. 우리는 지금 외부에 있는 서버에 ssh로 연결한다. 그리고 key방식을 사용해서 로그인하기 때문에 pem 파일에 대해 다음과 같은 권한 설정이 필요하다. 

 

(아래쪽 게시글에 윈도우용 세팅 답변 있음)

https://stackoverflow.com/questions/9270734/ssh-permissions-are-too-open-error

 

ssh "permissions are too open" error

I had a problem with my mac where I couldn't save any kind of file on the disk anymore. I had to reboot OSX lion and reset the permissions on files and acls. But now when I want to commit a reposi...

stackoverflow.com

aws instance에 들어가면 chmod 400으로 맞추는(owner만 read) 걸 제안하기 때문에, 이에 맞게 설정하면 된다. 이를 설정하지 않으면 permissions are too open 문구가 뜰 것이다.

 

2. 그리고 ssh config 파일에 대해서도 권한 설정이 필요하다.

https://stackoverflow.com/questions/49926386/openssh-windows-bad-owner-or-permissions

 

openssh windows bad owner or permissions

I've installed openssh for windows and when I run ssh localhost I get Bad owner or permissions on C:\Users\gary/.ssh/config I've looked at these 2 questions https://superuser.com/questions/348...

stackoverflow.com

이 에러의 왜 발생하는지, 그리고 해결법 또한 이해가 잘 되진 않지만, ssh config가 적힌 파일의 adminitrator의 권한을 삭제만 하면 된다. 설정하지 않으면 다음과 같은 에러가 발생한다.

Error running command remote-containers.attachToRunningContainerFromViewlet: Shell server terminated (code: 255, signal: null) Bad owner or permissions on C:\\Users\\username/.ssh/config . This is likely caused by the extension that contributes remote-containers.attachToRunningContainerFromViewlet.

연결

vscode에서 f1을 누르고 remote-ssh를 입력하거나 왼쪽 하단의 초록색 부분을 클릭하면 나음과 같은 화면이 나오는데, Connect to Host를 클릭한다.

그리고 ssh config 파일을 선택하고 원하는 서버를 선택한다. (만약 서버 설정이 되어있지 않다면 하기 내용을 config 파일에 입력하면 된다.)

Host [AWS]dapada-edu-basic
    HostName xxx.xxx.xxx.xxx # 서버 ip(domain name도 된다)
    User ubuntu # 접속할 유저이름
    Port 22 # 접속할 ssh 포트
    IdentityFile x:\xxx\xxx\xxx\xxx.pem # 사용할 pem 파일 경로

해당 서버에 ssh로 vscode가 연결 됐다면 docker extension을 설치해준다.

그러면 아래와 같이 탭이 생긴다. (도커 컨테이너는 실행되고 있어야 해당 목록이 뜬다)

그리고 원하는 컨테이너에 우클릭하여 attach visual studio code를 선택하면 연결이 완료된다!

결론

현재 노트북으로 m1 mac air를 사용중인데, remote container에 자동으로 붙었다. 아마 open ssh가 기본적으로 깔려있고, config 파일들의 권한에도 문제가 없어서 자연스럽게 연결이 된게 아닐까 생각한다.