2025.06.04
이슈
- 요약
- Docker 컨테이너 내부에서, 호스트 머신이 포트포워딩 중인 DB에 접근해야한다.
- 문제 상황 상세
- DB를 다른 서버(타사 서버, aws document DB 등)에서 실행 중이며, 해당 서버에서만 접근 가능하도록 설정 되어 있다.
- 해당 서버로 ssh 터널링을 하면, localhost로 DB에 접근할 수 있다.
- Docker 컨테이너에서 직접 터널링 하는 것은 적절치 않다.
- 터널링은 호스트 머신에서 하고, Docker 컨테이너는 호스트의 localhost를 참조해야한다.
- 위와 같은 상황에서, 맥북 로컬에서는 문제가 없었으나, production 서버(GCP VM, ubuntu)에서는 에러가 발생하였다.
마주한 케이스, 각 개발 환경
경우 1
- GCP VM에서 AWS Documnet DB 접근하도록 할 때 한 번 진행한 바 있음. (Nest JS, Python) - 2024.11 즈음…
241121 GCP host에서 mongo SSH 시도
- AWS→GCP로 서비스 이동. 단, document DB는 그대로 사용. AWS EC2에서는 다른 설정 없이 접근 가능했지만, 외부 머신은 그렇지 않음.
- 이땐 잘 해결 됨. docker gateway IP 사용해서…
- 개발 환경
- AWS, GCP
- Ubuntu
- Python, Nest JS
- mongo DB
경우 2
- Python. 타사 서버에서 실행 중인 DB에 접근해야됨. - 2025.05
- 이전에 했던 방식으로 해결 시도했으나, 잘 안 됨 (produciton 에서만 에러 발생). 결국 타사 서버에 BE 띄움.
- 개발 환경
- GCP
- Ubuntu
- Python
- maria db
참고
- https://velog.io/@1109_haeun/MySQL-Error-syncing-database-ConnectionRefusedError-SequelizeConnectionRefusedError-connect-ECONNREFUSED-13306
- https://velog.io/@mero/docker에서-nest와-mysql-연결-안되는-문제
결론 (해결 버전)
<언젠가 쓸 것.>
과정
SSH 터널링
ssh -o StrictHostKeyChecking=no -i /<경로>/<이름>.pem -f -N -L <로컬 포트 : 원격 서버 접속 : 원격 서버 포트> <서버 주소>
- -o StrictHostKeyChecking=no
- 참고: 원격 접속 전, known_hosts 파일에 저장된 호스트 키와 비교하는 작업이 진행됨(보안상의 이유로). 갖고있던 키와 다르다면, 오류가 발생함.
- =no로 호스트 키를 검증하지 않도록 설정.
- -N
- 원격 명령이 실행되지 않을 것임을 의미함. SSH가 설정한 터널 외에는 아무 것도 수행하지 않도록 함.
- -L
- 지정한 로컬 포트가, 지정한 호스트 서버의 지정한 포트로 연결되어야함.
Docker의 network 에 대해서 알아봄
- https://bluese05.tistory.com/53
- https://lasel.kr/archives/773
- docker compose.yml 파일의 <network_mode: "host”> 란?
- network_mode: 컨테이너의 네트워크 모드를 지정함. (bridge, host, none, service:~, container:~ 등…) 그중 host는 컨테이너 안에서 호스트의 네트워크를 그대로 사용함.
🔎 extra_hosts
host.docker.internal 에 대해서 알아봄
- https://lasel.kr/archives/773
- https://stackoverflow.com/questions/48546124/what-is-the-linux-equivalent-of-host-docker-internal
- https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container/61424570#61424570
- https://docs.docker.com/desktop/features/networking/#use-cases-and-workarounds-for-all-platforms
- https://stackoverflow.com/questions/35720529/connect-robomongo-to-mongodb-docker-container
- https://github.com/docker/for-linux/issues/264
🔎 host.docker.internal db
- https://marklee1117.tistory.com/93
- https://shawn-dev.oopy.io/463a30bf-bf32-44e4-88be-8b4722e5549a
- https://non-stop.tistory.com/718
- https://yoo11052.tistory.com/143
- https://ohjinn.tistory.com/97
- 가상화된 DB 등과의 컨테이너 통신: 노드 서버와 몽고db 두 개 모두 컨테이너에서 돌린다고 생각해보자. 둘 다 호스트 머신이 아니니 host.docker.internal 명령어는 통하지 않을 것이다. 우선 몽고db를 띄워보면
🔎 host.docker.internal db ssh
Docker Gateway 확인
(venv) dev@prd2:~/neural/soyeon$ sudo ip addr show docker0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:c0:9f:b6:fe brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:c0ff:fe9f:b6fe/64 scope link
valid_lft forever preferred_lft forever
호스트 머신의 IP 얻는법
ip route get 1 | sed -n 's/^.*src \\([0-9.]*\\) .*$/\\1/p')
- GCP 에서 실행하니까 내부 IP 출력됨.
'DevOps > Docker' 카테고리의 다른 글
| [Docker] docker, docker compose 설치 (0) | 2025.09.03 |
|---|---|
| [Docker][Error]‘Killed’ error (0) | 2025.09.03 |