개발 환경/Pycharm

Pycharm에서 server의 docker 사용해서 디버깅하기 (무슨 말?)

HanSeokhyeon 2020. 9. 3. 17:47
반응형

제목을 쓰고 나니 나도 무슨 말인지 모르겠다.

 

0. 현재 상황 및 해결 방안

  • Docker와 Pycharm을 사용해서 개발중.
  • 평소에 local에서 디버깅하고, server(나의 경우 aws instance)에서 run 했음.
  • Local에서 디버깅하려는데 모델 크기가 너무 커서 내 맥북 램 8GB를 넘어버림.

 

Server에 존재하는 docker에 pycharm으로 접속해서 디버깅하자!

  1. Server에서 tcp 소켓을 이용해 port를 열고 docker를 실행 및 외부에서 접속 확인.
  2. Pycharm에서 docker 설정.
  3. 디버깅할 스크립트의 configuration 설정.
  4. 디버깅 시작!

 

1. Server에서 tcp 소켓을 이용해 port를 열고 docker를 실행 및 외부에서 접속 확인.

1. socat 설치

설치가 안되어 있다면 하자.

sudo apt update
sudo apt install socat

2. Server에서 2375 port로 docker 실행 및 확인.

sudo service docker stop
sudo dockerd -H tcp://127.0.0.1

우선 localhost로 제대로 작동하는지 확인해보자. 위 명령어를 치면 2375 port를 listening할 것이다.

output:

INFO[2020-09-03T08:23:06.787968349Z] Starting up
WARN[2020-09-03T08:23:06.788307562Z] [!] DON'T BIND ON ANY IP ADDRESS WITHOUT setting --tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING [!]
INFO[2020-09-03T08:23:06.788599863Z] detected 127.0.0.53 nameserver, assuming systemd-resolved, so using resolv.conf: /run/systemd/resolve/resolv.conf
INFO[2020-09-03T08:23:06.789160478Z] parsed scheme: "unix"                         module=grpc
INFO[2020-09-03T08:23:06.789181780Z] scheme "unix" not registered, fallback to default scheme  module=grpc
INFO[2020-09-03T08:23:06.789210130Z] ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock 0  <nil>}] <nil>}  module=grpc
INFO[2020-09-03T08:23:06.789225458Z] ClientConn switching balancer to "pick_first"  module=grpc
INFO[2020-09-03T08:23:06.790210420Z] parsed scheme: "unix"                         module=grpc
INFO[2020-09-03T08:23:06.790227810Z] scheme "unix" not registered, fallback to default scheme  module=grpc
INFO[2020-09-03T08:23:06.790255238Z] ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock 0  <nil>}] <nil>}  module=grpc
INFO[2020-09-03T08:23:06.790265154Z] ClientConn switching balancer to "pick_first"  module=grpc
INFO[2020-09-03T08:23:06.792661817Z] [graphdriver] using prior storage driver: overlay2
WARN[2020-09-03T08:23:06.820060760Z] Your kernel does not support swap memory limit
WARN[2020-09-03T08:23:06.820078669Z] Your kernel does not support cgroup rt period
WARN[2020-09-03T08:23:06.820094108Z] Your kernel does not support cgroup rt runtime
WARN[2020-09-03T08:23:06.820103767Z] Your kernel does not support cgroup blkio weight
WARN[2020-09-03T08:23:06.820127441Z] Your kernel does not support cgroup blkio weight_device
INFO[2020-09-03T08:23:06.820272451Z] Loading containers: start.
INFO[2020-09-03T08:23:06.922159075Z] Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address
INFO[2020-09-03T08:23:06.961662845Z] Loading containers: done.
INFO[2020-09-03T08:23:06.992294117Z] Docker daemon                                 commit=369ce74a3c graphdriver(s)=overlay2 version=19.03.6
INFO[2020-09-03T08:23:06.992334278Z] Daemon has completed initialization
INFO[2020-09-03T08:23:07.006557395Z] API listen on 127.0.0.1:2375

Server의 다른 터미널 하나 켜서 확인해보자.

docker -H tcp://127.0.0.1 images

output:

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
hsh0322/kcbert        latest              629c1380cef8        23 minutes ago      7.69GB

2375 port를 통해 docker가 정상적으로 동작하는 것을 확인하였다.

3. 외부로 노출시키기

sudo dockerd -H tcp://xxx.xxx.xxx.xxx

공인 IP로 dockerd를 실행하면 외부로 노출된다.

sudo dockerd -H tcp://ec2-x-x-x-x-....

주의할 점은 AWS instance는 보안때문에 2375 port가 외부로 노출이 안되어 있다. 그러므로 dashboard - security groups - inbound rule 추가를 하면 된다. 구글에 aws ec2 inbound tcp 이런 식으로 검색하면 설명이 잘 나올 것이다. 그리고 도메인으로 접속해야 한다.

4. Local에서 접속 확인

Local에서 시도하자.

docker -H tcp://xxx.xxx.xxx.xxx images

output:

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
hsh0322/kcbert        latest              629c1380cef8        23 minutes ago      7.69GB

2375 port를 통해 docker가 정상적으로 동작하는 것을 확인하였다.

 

2. Pycharm에서 docker 설정

설정에서 docker에 들어가면 이러한 설정을 볼 수 있다. 위처럼 TCP socket에 위와 같이 적어주면 된다.

tcp://xxx.xxx.xxx.xxx:2375

그러면 위처럼 연결할 수 없다고 안나오고 잘 연결된다고 뜰 것이다.

 

3. 디버깅할 스크립트의 configuration 설정.

이게 제일 어렵다.

우선 위와 같이 docker interpreter를 만들자. 설정에서 만들 수 있다.

하나씩 살펴보자.

  • Script path: /opt/project/main.py - 컨테이너 내부에 스크립트의 위치를 말한다. Server에서 마운트 시킬 계획이 아니라면 컨테이너에 코드가 존재해야 한다. (마운트를 하든가 매번 다시 빌드를 하든가)
  • Python interpreter: Remote Python 3.7.7 Docker - 위에서 만든 interpreter가 되어야 한다.
  • Docker container settings: --rm --gpus all - docker container에 사용할 옵션을 적어주자.
  • [2020.09.22 수정] Path mappings: /Users/hanseokhyeon/bwai/src/module/kcBERT=/opt/project

 

4. 디버깅 시작!

아마 하면 될 것이다... 머 동작이 안되면 능력껏 트러블 슈팅하면서 해결하다보면 분명 될 것이다!

반응형