먼저, 이번에 docker 에 대해 공부하며 익숙해진 명령어를 정리한다.
cf)
centos:7.9 => PostgreSQL가 설치된 컨테이너
docker.io/continuumio/anaconda3:latest : Anaconda3 가 설치된 컨테이너
$ docker network create [new_network_name]
$ docker network create docker-net
$ docker network ls
$ docker run -d --name conda --network [network_name][docker_image]
$ docker run -d --name conda --network docker-net docker.io/continuumio/anaconda3
$ docker network connect [network_name][container_name]
$ docker network connect docker-net affectionate_sinoussi
$ docker network connect docker-net condescending_goodall
$ 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
$ docker exec -it [container_id] /bin/bash
$ docker exec -it b1eea4f5345a /bin/bash
$ docker commit [container_name][new_reposiory_name]:[new_tag]
$ docker commit sharp_swanson ag_people:latest
$ 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
yum install -y bridge-utils
brctl show(docker0 가 브릿지 인터페이스인 것을 확인할 수 있다)
apt-get install -y iputils-ping
apt-get install -y vim
centos 컨테이너 안에 생성된 AgensGraph(=PostgreSQL) DB내의 People table의 데이터를 Anaconda3 가 설치된 컨테이너에서 조회
Docker 컨테이너 간 네트워크 연결에서는 굳이 docker network를 새로 만들어 IP를 새로 만들 필요가 없다. 각 컨테이너에 주어지는 개별 IP를 이용해서 연결하면 된다.
centos(PG) | conda | |
---|---|---|
eth0 | 172.17.0.2 | 172.17.0.3 |
eth1 | 172.18.0.2 | 172.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)
현재 실행중인 컨테이너 Image 만들기
$ docker commit [container_name][new_reposiory_name]:[new_tag]
$ docker commit sharp_swanson ag_people:latest
$ 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 ')]
=> 성공