최근 도커를 공부하다가 다른 컨테이너에 있는 프로세스끼리 어떤 식으로 통신하는지 궁금해져서 테스트를 해봤다. 

 
도커를 통해 두 개의 컨테이너를 만들 건데 하나는 nodejs 하나는 mongodb을 띄울 거다.


움 완성하고 보니 대충 이런 그림?


코드는 아래와 같이 진행하면 된다.


완성하고 보니 link로 연결하면 컨테이너의 /etc/hosts  에 다른 컨테이너 아이디가 등록된 것을 확인할 수 있었다.


도커프록시를 통해 내부에서 할당받은 아이피대역대끼리는 통신이 되기 때문에 이런 식으로 가능한 듯 ..


문득 궁금한게 k8s에서는 pod간 통신이 궁금해진.. ( 다음 포스팅에 올려볼예정 )


** 이 포스팅에 잘못 된 내용이 있으면 지적해주세요 


nodejs-mongo-test

First : mongodb을 시작한다. ( 여기선 docker를 이용하여 시작 ).


몽고디비 컨테이너에 생성

$ docker run --name mongo -p 27017:27017 -d mongo

도커 프로세스 확인

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
b2482f6a58be        mongo               "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        0.0.0.0:27017->27017/tcp   mongo

컨테이너에 접속

$ docker exec -it mongo /bin/bash

mongo를 입력하여 제대로 동작하는지 확인

root@b2482f6a58be:/# mongo

MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
Server has startup warnings:
2018-06-30T04:51:49.681+0000 I STORAGE  [initandlisten]
2018-06-30T04:51:49.681+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-06-30T04:51:49.681+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-06-30T04:51:50.154+0000 I CONTROL  [initandlisten]
2018-06-30T04:51:50.154+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-30T04:51:50.154+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-06-30T04:51:50.154+0000 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service to collect and display
metrics about your deployment (disk utilization, CPU, operation statistics,
etc).

The monitoring data will be available on a MongoDB website with a unique
URL created for you. Anyone you share the URL with will also be able to
view this page. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command:
db.enableFreeMonitoring()
>

>  db
test

Second : nodejs 실행


우선 해당 git에 프로젝트 다운

$ git clone https://github.com/yangjeehan/nodejs-mongo-test.git 

해당 도커파일을 통해 이미지를 만든다.

$ docker build -t nodejs-mongodb-test:yang

*만든 이미지를 실행한다. ( 전에 만든 mongo container을 link을 통해 연결 ) *

$ docker run -i --link mongo --name test -t nodejs-mongodb-test:yang /bin/bash 

만든 컨테이너에 접속하여 아래명령어 수행

$ docker exec -it test /bin/bash 
root@ee137a79dd4e:/usr/src/app# node demo_mongo_insert.js 

몽고디비에 접속해서 데이터 입력 체크

root@b2482f6a58be:/# mongo

> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB

> use mydb
switched to db mydb
>
> show collections;
customers

> db.customers.find()
{ "_id" : ObjectId("5b371eb7d8daba00078e2fdf"), "name" : "Company Inc", "address" : "Highway 37" }

End


'apps > docker' 카테고리의 다른 글

도커란 무엇인가?  (0) 2018.08.04
flannel을 통한 pod간 통신  (2) 2018.07.03
kubernetes에서 gpu pod생성( nvidia-docker2 )  (2) 2018.05.30
centos7에서 nvidia driver설치하기  (0) 2018.05.28
kubenetes설치 실패 시 초기화방법  (0) 2018.05.28

+ Recent posts