docker_container 간의 연결

Ja L·2023년 3월 20일
0

docker

목록 보기
4/4

먼저, 이번에 docker 에 대해 공부하며 익숙해진 명령어를 정리한다.

cf)
centos:7.9 => PostgreSQL가 설치된 컨테이너
docker.io/continuumio/anaconda3:latest : Anaconda3 가 설치된 컨테이너

Docker network 만들기

$ docker network create [new_network_name]
$ docker network create docker-net

Docker network 리스트 조회

$ docker network ls


Container 에 network 설정 부여

(컨테이너 생성 시 네트워크 설정 부여하는 경우)

$ docker run -d --name conda --network [network_name][docker_image]
$ docker run -d --name conda --network docker-net docker.io/continuumio/anaconda3

Container 에 network 설정 부여

(컨테이너 생성 후 네트워크 설정 부여하는 경우)

$ docker network connect [network_name][container_name]
$ docker network connect docker-net affectionate_sinoussi
$ docker network connect docker-net condescending_goodall

Container 실행 (container 내부로 접속)

$ docker run -i -t [REPOSITORY]:[TAG] /bin/bash
$ docker run -i -t centos:7.9 /bin/bash
$ docker run -i -t docker.io/continuumio/anaconda3 /bin/bash

cf) Container 실행을 종료하지 않고 탈출
Ctrl + p + q

실행중인 Container 내부로 접속

$ docker exec -it [container_id] /bin/bash
$ docker exec -it b1eea4f5345a /bin/bash

현재 실행중인 컨테이너 Image 만들기

$ docker commit [container_name][new_reposiory_name]:[new_tag]
$ docker commit sharp_swanson ag_people:latest

Container에 Port 개방 & 실행

$ docker run -p [hostport]:[guestport] -i -t [REPOSITORY]:[TAG] /bin/bash
$ docker run -p 5432:80 -i -t centos:7.9 /bin/bash

PostgreSQL default port : 5432
Http default port : 80


network bridge 설치

yum install -y bridge-utils

network bridge 확인

brctl show(docker0 가 브릿지 인터페이스인 것을 확인할 수 있다)

ubuntu에서 ping 명령어 설치

apt-get install -y iputils-ping

ubuntu에서 vi 명령어 설치

apt-get install -y vim




본론

목표:

centos 컨테이너 안에 생성된 AgensGraph(=PostgreSQL) DB내의 People table의 데이터를 Anaconda3 가 설치된 컨테이너에서 조회



Docker 컨테이너 간 네트워크 연결에서는 굳이 docker network를 새로 만들어 IP를 새로 만들 필요가 없다. 각 컨테이너에 주어지는 개별 IP를 이용해서 연결하면 된다.

centos(PG)conda
eth0172.17.0.2172.17.0.3
eth1172.18.0.2172.18.0.3

현재 네트워크 상황은 위 표와 같은데 eth1은 $ docker network connect docker-net 명령어로 docker-net이라는 네트워크를 생성하고 각각 컨테이너에 설정하여 생긴 IP이다. 이러한 과정을 하지 않아도 된다는 뜻이다.

우리는 eth0을 통해 컨테이너 간 연결을 진행할 것이다.

우선, $AGDATA 디렉토리에 가서 vi postgresql.conf 설정을 변경한다.
listen_addresses="*"

마찬가지로 vi pg_hba.conf 설정을 변경한다.
# IPv4 local connectos:
host all all 0.0.0.0/0 trust

AgensGraph를 실행한다.
$ ag_ctl start

이후 centos 컨테이너 내의 AG에 people 테이블을 만들고 5개의 샘플 데이터를 만든다.
# agens -U agens -d postgres
# CREATE TABLE people(
sex char(10),
name char(10)
)
;

# INSERT INTO people VALUES('male', 'lee');
# INSERT INTO people VALUES('female', 'ki');
# INSERT INTO people VALUES('female', 'song');
# INSERT INTO people VALUES('male', 'cho');
# INSERT INTO people VALUES('female', 'kim');

# SELECT * FROM people ;
sex | name
------------+------------
male | lee
female | ki
female | song
male | cho
female | kim
(5 rows)


테이블과 데이터를 생성했다.
컨테이너의 port가 열리지 않은 상태이다. 여기까지의 데이터를 Docker Image로 만들고 해당 Image를 통해 새로운 컨테이너를 만들면서 port를 열어준다.

현재 실행중인 컨테이너 Image 만들기
$ docker commit [container_name][new_reposiory_name]:[new_tag]
$ docker commit sharp_swanson ag_people:latest

Port 를 개방하며 Container 실행시키기

$ docker run -p [hostport]:[guestport] -i -t [REPOSITORY]:[TAG] /bin/bash
$ docker run -p 5432:80 -i -t ag_people:latest /bin/bash


ctrl + p + c 를 이용하여 centos 컨테이너에서 빠져나온다.


Anaconda3 가 설치된 컨테이너로 진입
$ docker exec -it b1eea4f5345a /bin/bash


psyco.py 파일을 생성한다.
$ touch test_psyco.py

편의상 vim 편집기를 설치한다.
$ apt-get install vim

pip3 업데이트를 진행한다.
$ pip install --upgrade pip

psycopg2를 설치한다.
$ pip3 install psycopg2-binary

test_psyco.py 파일을 작성한다.
$ vi test_psyco.py

# test_psyco.py

import psycopg2

conn=psycopg2.connect(
database="postgres",
host="172.17.0.2",
user="agens",
password=""
)

cur=conn.cursor()

cur.execute("select * from people ;")

print(cur.fetchall())

cur.close()
conn.close()

[('male ', 'lee '),
('female ', 'ki '),
('female ', 'song '),
('male ', 'cho '),
('female ', 'kim ')]

=> 성공

profile
DB Engineer

0개의 댓글