標籤:
參考:https://hub.docker.com/_/postgres/
https://docs.docker.com/engine/installation/debian/
利用Docker進行多執行個體的安裝,相對於源碼安裝更簡單,更方便,隔離性也更好 。
安裝Docker
- 更新安裝源
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609Dsudo touch /etc/apt/sources.list.d/docker.listsudo vim /etc/apt/sources.list.d/docker.list
根據系統寫入:
deb https://apt.dockerproject.org/repo debian-jessie main
- 安裝
sudo apt-get updatesudo apt-get install docker-engine
- 開啟服務
sudo service docker start
- 驗證
sudo docker run hello-world該命令會下載一個測試鏡像,並在容器內運行,列印出相關資訊,當看到類似如下資訊是表示運行正常。Hello from Docker.This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.
安裝PostgreSQL
- 下載最新官方PostgeSQL鏡像
sudo docker pull postgres
- 啟動 兩個鏡像,分別映射至宿主機5454及5455連接埠
sudo docker run --name cluster1 -p 5454:5432 -e POSTGRES_PASSWORD=postgres -d postgressudo docker run --name cluster2 -p 5455:5432 -e POSTGRES_PASSWORD=postgres -d postgres
- 查看運行中的容器及相關資訊
[email protected]:~$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3bf3b533ba05 postgres "/docker-entrypoint.s" 26 seconds ago Up 24 seconds 0.0.0.0:5455->5432/tcp cluster229b9ffeb6ac8 postgres "/docker-entrypoint.s" 58 seconds ago Up 56 seconds 0.0.0.0:5454->5432/tcp cluster1[email protected]:~$ sudo docker inspect 3bf3b533ba05 |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3",[email protected]:~$ sudo docker inspect 29b9ffeb6ac8 |grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2",
- 根據上一步中的IPAddress,進行串連測試
[email protected]:~$ psql -h172.17.0.2 -Upostgres -p5432Password for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres(3 rows)[email protected]:~$ psql -h172.17.0.3 -Upostgres -p5432Password for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres(3 rows)通過宿主機地址和連接埠串連資料庫[email protected]:~$ psql -h192.168.10.131 -p5454 -UpostgresPassword for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres(3 rows) postgres=# \q[email protected]:~$ psql -h192.168.10.131 -p5455 -UpostgresPassword for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres(3 rows)
查看容器內資料檔案、設定檔、網路授權檔案設定
sudo docker exec -it 3bf3b533ba05 /bin/bash所有postgresql設定檔和資料資料檔案位於:/var/lib/postgresql/data
PostgreSQL 基於Docker的多執行個體安裝