Docker:PostgreSQL-11配置数据持久化
卷的原理图:
主机中的本地目录作为Docker容器内的持久存储卷装载,以便在主机和Docker容器之间共享数据。如果主机希望访问或定期备份在Docker容器内运行的DB服务器写入文件夹的数据或数据库,则此方法非常有用。
创建本地数据卷
#创建本地卷
[root@tar1 ~]# docker volume create pgdata
create : 后面写自定义卷名
创建并运行容器
[root@tar1 ~]# docker run -v pgdata:/root/data --name dockerPG11 -e POSTGRES_PASSWORD=Xzzp2008 -p 54322:5432 -d postgres:11.5 acb5bf2a4a26b525b8ef4327efcea31963bde8f8ed78ee57f4bc98d8be391f4c
-v:pgdata 是刚刚创建的本地卷名称
卷(volume)是绕过容器的文件系统,直接将数据写到host主机上,只是volume是被docker管理的,docker下所有的volume都在host机器上的指定目录下/var/lib/docker/volumes。
[root@tar1 volumes]# ll /var/lib/docker/volumes 总用量 32 drwxr-xr-x. 3 root root 19 8月 23 18:27 14145904c49f663d8345ef789a9c352046a6a6d1ccd49f4d9e2dfd1a141f6707 drwxr-xr-x. 3 root root 19 8月 23 21:26 54943648b14cc92532aa20c95e8c7d70404a344eb427750aefd873b1b2003c74 drwxr-xr-x. 3 root root 19 8月 23 17:40 59aca22a107ede60abc1795edd9d7a46ff5799850a43f536a91598f74ffa7e01 drwxr-xr-x. 3 root root 19 8月 23 17:46 666f867e5062405683033d419458a85944568a007d6e82720149f48d306c4741 drwxr-xr-x. 3 root root 19 8月 23 17:26 7b3400e3155b8a3f3950f6a495883532de20270c9811de7ea0ab5e31d944a165 drwxr-xr-x. 3 root root 19 8月 23 17:42 8f56e930685166205e4d9fa5730f844aa7b0e0d1dc503e4ec4dd529023a5e17a drwxr-xr-x. 3 root root 19 8月 23 17:43 9cc314ca59489507b54a0f9122d9ce4fc929c99a885dab2bcd8b42df516cfa27 drwxr-xr-x. 3 root root 19 8月 23 17:48 c5c58f45d54442366d2dff3d939988601a9b46c57d2a750ebeb3c087d1e27d10 drwxr-xr-x. 3 root root 19 8月 23 19:43 d07687b3480ad42bc1d66fc7f609e831d10e917dbc6f95447af69113e8448d56 -rw-------. 1 root root 65536 8月 23 21:26 metadata.db drwxr-xr-x. 3 root root 19 8月 23 19:43 pgdata
测试持久化
查看容器
[root@tar1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES acb5bf2a4a26 postgres:11.5 "docker-entrypoint..." 9 seconds ago Up 8 seconds 0.0.0.0:54322->5432/tcp dockerPG11
进入容器
[root@tar1 ~]# docker exit -it acb5bf2a4a26 bash
登录数据库
#切换postgres用户 [root@acb5bf2a4a26 ~]# su postgres #登录数据库 [postgres@acb5bf2a4a26 ~]# psql -U postgres -W postgres=#
创建数据表
postgres=# CREATE TABLE season (id int, name varchar(80)); CREATE TABLE ^ postgres=# INSERT INTO season (id,name) VALUES (1, 'March'); INSERT 0 1 postgres=# SELECT * FROM season; id | name ----+------- 1 | March (1 行记录) postgres=# CREATE TABLE t_test (x numeric); CREATE TABLE postgres=# INSERT INTO t_test SELECT random() postgres-# FROM generate_series(1, 50000000); INSERT 0 50000000 postgres=# select * from t_test limit 5; x -------------------- 0.0994161088019609 0.887578224763274 0.232774924486876 0.416146846953779 0.557969538029283 (5 行记录)
退出容器
#快捷键
Ctrl + P + Q
重启容器
#关闭容器
[root@tar1 ~]# docker stop acb5bf2a4a26 acb5bf2a4a26
#启动容器 [root@tar1 ~]# docker start acb5bf2a4a26 acb5bf2a4a26
进入容器查看数据是否存在
postgres=# SELECT * FROM season; id | name ----+------- 1 | March (1 行记录)
持久化成功。
docker查看卷列表
#查看卷列表命令 [root@tar1 ~]# docker volume ls
[root@tar1 ~]# docker volume ls DRIVER VOLUME NAME local 14145904c49f663d8345ef789a9c352046a6a6d1ccd49f4d9e2dfd1a141f6707 local 59aca22a107ede60abc1795edd9d7a46ff5799850a43f536a91598f74ffa7e01 local 666f867e5062405683033d419458a85944568a007d6e82720149f48d306c4741 local 7b3400e3155b8a3f3950f6a495883532de20270c9811de7ea0ab5e31d944a165 local 8f56e930685166205e4d9fa5730f844aa7b0e0d1dc503e4ec4dd529023a5e17a local 9cc314ca59489507b54a0f9122d9ce4fc929c99a885dab2bcd8b42df516cfa27 local c5c58f45d54442366d2dff3d939988601a9b46c57d2a750ebeb3c087d1e27d10 local d07687b3480ad42bc1d66fc7f609e831d10e917dbc6f95447af69113e8448d56 local volnamepg
查看指定卷信息
#指定卷信息命令
[root@tar1 ~]# docker volume inspect pgdata
[root@tar1 ~]# docker volume inspect pgdata [ { "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/pgdata/_data", "Name": "pgdata", "Options": {}, "Scope": "local" }
删除卷
#删除卷命令 [root@tar1 ~]# docker volume rm pgdata
问题总结
问题1:
WARNING: IPv4 forwarding is disabled. Networking will not work.
解决方法
#宿主机修改配置文件 [root@tar1]# vim /usr/lib/sysctl.d/00-system.conf 添加如下代码: net.ipv4.ip_forward=1 #重启network服务 [root@tar1]# systemctl restart network
问题2:
卷删除失败
[root@tar1 ~]# docker volume rm cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f Error response from daemon: unable to remove volume: remove cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f: volume is in use - [83fd1e1408100846bc19348f6f01f5250a8881831d2383047a72fa9b26672701]
解决方法
卷正在被容器使用,要先删除容器
问题3:
容器ID冲突
[root@tar1 yum.repos.d]# docker run --rm --name dockerPG11 -e POSTGRES_PASSWORD=Xzzp2008 -p 54322:54355 -d postgres:11.5 docker: Error response from daemon: Conflict. The container name "/dockerPG11" is already in use by container "df222cdf3efb532da55c33d63ec98d0d01f10b9927dc7d4f9704633e42669874". You have to remove (or rename) that container to be able to reuse that name. See 'docker run --help'.
解决方法
#获取容器ID docker ps –a #删除容器 docker rm df222cdf3efb
问题4:
docker: Error response from daemon: driver failed programming external connectivity on endpoint dockerPG11
[root@tar1 yum.repos.d]# docker run --name dockerPG11 -e POSTGRES_PASSWORD=Xzzp2008 -p 5432:54355 -d postgres:11.5 973e9066cefee0747fc51458060ec37168039a005a23b406f787e5ad9dee146a docker: Error response from daemon: driver failed programming external connectivity on endpoint dockerPG11 (00b10c3636525f1a7c7c9ee8c30ad74c9d153be32d93fb02b01e028b05f3db55): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5432 -j DNAT --to-destination 172.17.0.2:54355 ! -i docker0: iptables: No chain/target/match by that name. (exit status 1)).
解决方法
#重启docker
systemctl restart docker
-----------------------------------
作者:怒吼的萝卜
链接:http://www.cnblogs.com/nhdlb/
-----------------------------------