docker下创建centos7容器
1.创建网络
docker network create --subnet=172.19.0.0/16 pgnetwork
docker network ls -- 查看 此处使用pgnetwork
NETWORK ID NAME DRIVER SCOPE
d725415b8b96 bridge bridge local
e4012194a61b host host local
4234dac60f16 none null local
e73e04a89c47 pgnetwork bridge local
f8c3a20c67c2 rabbitmqnet bridge local
docker network inspect pgnetwork --查看网络内部信息
2.docker pull centos -- 下载latest的 centos 镜像,此处镜像重命名为centos:7
3.创建容器 pg_ip_ms,pg_ip_s,映射端口5300,5301
docker run -itd --privileged -v /sys/fs/cgroup:/sys/fs/cgroup -p 5370:5432 --name pg_ip_ms --net pgnetwork --ip 172.19.0.5 centos:7 /usr/sbin/init
--privileged container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
privileged启动的容器,可以看到很多host上的设备,并且可以执行mount。甚至允许你在docker容器中启动docker容器。
--net pgnetwork 指定网络
--ip 172.19.0.2 指定IP
4.进入容器
docker exec -it pg_ip_ms /bin/bash
5.install postgresql
postgresl install
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm -- 下载rpm
yum install postgresql96 -- install client
yum install postgresql96-server postgresqyl96-contrib -- install sderver和第三方插件
/usr/pgsql-9.6/bin/postgresql96-setup initdb -- 初始化数据库
systemctl enable postgresql-9.6 --开机自启
systemctl start postgresql-9.6 -- 启动服务
完成以上操作后可以登陆postgresql试试
su postgres
psql 可以登陆试试,之后修改配置文件
修改配置文件postgresql.conf
listen_addresses ='*'
port=5432
password_encryption = on
修改pg_hba.conf
host all all 0.0.0.0/0 md5.
host all all 127.0.0.1/32 md5.
systemctl restart postgresql-9.6
登陆数据库su - postgres
psql
alter user postgres with password '123456';
6.install pglogical pglogical-rhel-1.0-3.noarch.rpm
从主机复制到容器sudo docker cp host_path containerID:container_path
从容器复制到主机sudo docker cp containerID:container_path host_path
docker cp pglogical-rhel-1.0-3.noarch.rpm pg_xxx:/home/
yum install pglogical-rhel-1.0-3.noarch.rpm
yum install postgresql96-pglogical
vi /var/lib/pgsql/9.6/data/postgresql.conf
修改配置文件postgresql.conf
listen_addresses ='*'
port=5432
password_encryption = on
shared_preload_libraries = 'pglogical'
max_worker_processes = 10 # one per database needed on provider node
wal_level = 'logical'
max_wal_senders = 10 # one per node needed on provider node
max_replication_slots = 10 # one per node needed on provider node
track_commit_timestamp = on
vi /var/lib/pgsql/9.6/data/pg_hba.conf
修改pg_hba.conf
host all all 0.0.0.0/0 trust.
local replication postgres trust
host replication postgres 127.0.0.1/32 trust
host replication postgres ::1/128 trust
host replication postgres 0.0.0.0/0 trust
systemctl restart postgresql-9.6
su - postgres
psql
CREATE EXTENSION pglogical; -- create 扩展工具
select extname, extowner from pg_extension; --查看扩展工具
7.installpg_pathman
git clone https://github.com/postgrespro/pg_pathman
下载到主机的root文件夹, 然后复制到容器内
对于bash: make: command not found错误
yum -y install gcc automake autoconf libtool make
对于make: pg_config: Command not found错误
find / -name pg_config --查看路径/usr/pgsql-9.6/bin/pg_config
export PATH=/usr/pgsql-9.6/bin:$PATH
cd pg_pathman
对于Makefile:66: /usr/pgsql-9.6/lib/pgxs/src/makefiles/pgxs.mk: No such file or directory错误
yum install postgresql96-devel
对于fatal error: openssl/ssl.h: No such file or directory #include
类似问题参考
yum install openssl
yum install openssl-devel
make USE_PGXS=1
make USE_PGXS=1 install
修改配置文件postgresql.conf
vi /var/lib/pgsql/9.6/data/postgresql.conf
shared_preload_libraries = 'pg_pathman,pglogical'
systemctl restart postgresql-9.6
su - postgres
psql
create extension pg_pathman;
\dx
-==============================================================
通过以上过程,镜像层层叠加生成由centos:7包含postgresql,pglogical和pg_pathman的镜像。
10.0.0.96 image:pg_ip_pglogical_pathman
-==============================================================
docker commit container_id pg_ip_pglogical_pathman -- 生成镜像pg_ip_pglogical_pathman
在这个镜像中,数据库pg已经包含了pglogical和pg_pathman的配置包, 如果新建的数据库需要使用这两个插件 可以使用下列sql语句添加
create extension pglogical;
create extension pg_pathman;