作者简介
赵振平,PostgreSQL中文社区主席、计算机畅销书作家、贵州省省管专家、太阳塔科技CTO、国家首批大数据高级职称、腾讯最具价值专家(TVP)、电子工业出版社签约作家、出版了技术专著《Oracle数据库精讲与疑难解析》、出版了技术专著《成功之路:Oracle 11g学习笔记》、出版了技术专著《IT架构实录》。
背景介绍
Docker前几年席卷了整个互联网,Docker在互联网很多业务场景都有使用。最近一两年,一些传统行业也在尝试使用,并且看到不少案例。另外,我们一直担心,容器影响数据库的性能,根据近些年的亲身实践,容器对数据库性能的影响很小,也不会影响数据库的并行性。因此,想写一系列文章,简单介绍PostgreSQL在容器中的实践。
Docker基本概念
什么是Docker
Docker 是一个软件平台,让您可以快速构建、测试和部署应用程序。Docker 将软件打包成名为容器的标准化单元,这些单元具有运行软件所需的所有功能,包括库、系统工具、代码和运行时。使用 Docker,您可以将应用程序快速部署和扩展到任何环境中,并且知道您的代码将运行。
Docker 镜像
Docker 镜像是定义容器的只读模板。该镜像包含将运行的代码,包括这些代码需要的任何库和依赖关系的所有定义。
Docker 容器
Docker 镜像一旦实例化,就变成了容器。容器是正在运行的。
Docker和虚拟机的区别

两者的区别主要是下面两点:
1)多个Docker可以共享操作系统内核,而虚拟机不行。
2)虚拟机更加耗费资源
内核要求
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看CentOS 版本是否支持。
[root@tar1 ~]# uname -r
3.10.0-957.el7.x86_64
[root@tar1 ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
更新软件包
[root@tar1 ~]# yum -y update
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
---> 软件包 kernel-devel.x86_64.0.3.10.0-957.27.2.el7 将被 安装
---> 软件包 kernel-headers.x86_64.0.3.10.0-957.el7 将被 升级
---> 软件包 kernel-headers.x86_64.0.3.10.0-957.27.2.el7 将被 更新
---> 软件包 kernel-tools.x86_64.0.3.10.0-957.el7 将被 升级
---> 软件包 kernel-tools.x86_64.0.3.10.0-957.27.2.el7 将被 更新
---> 软件包 kernel-tools-libs.x86_64.0.3.10.0-957.el7 将被 升级
---> 软件包 kernel-tools-libs.x86_64.0.3.10.0-957.27.2.el7 将被 更新
---> 软件包 kexec-tools.x86_64.0.2.0.15-21.el7 将被 升级
---> 软件包 kexec-tools.x86_64.0.2.0.15-21.el7_6.4 将被 更新
---> 软件包 krb5-devel.x86_64.0.1.15.1-34.el7 将被 升级
---> 软件包 krb5-devel.x86_64.0.1.15.1-37.el7_6 将被 更新
---> 软件包 krb5-libs.x86_64.0.1.15.1-34.el7 将被 升级
---> 软件包 krb5-libs.x86_64.0.1.15.1-37.el7_6 将被 更新
---> 软件包 krb5-workstation.x86_64.0.1.15.1-34.el7 将被 升级
---> 软件包 krb5-workstation.x86_64.0.1.15.1-37.el7_6 将被 更新
---> 软件包 libatomic.x86_64.0.4.8.5-36.el7 将被 升级
---> 软件包 libatomic.x86_64.0.4.8.5-36.el7_6.2 将被 更新
创建仓库
[root@tar1 ~]# cat /etc/yum.repos.d/docker.repo
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
[root@tar1 ~]#
清理yum 缓存
[root@tar1 ~]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: base dockerrepo extras pgdg10 pgdg11 pgdg94 pgdg95 pgdg96 updates
Cleaning up list of fastest mirrors
[root@tar1 ~]#
缓存yum仓库
[root@tar1 ~]# yum makecache
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
base | 3.6 kB 00:00:00
dockerrepo | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
pgdg10 | 3.6 kB 00:00:00
pgdg11 | 3.6 kB 00:00:00
安装Docker包
[root@tar1 ~]# yum install -y docker-engine
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-engine.x86_64.0.17.05.0.ce-1.el7.centos 将被 安装
--> 正在处理依赖关系 docker-engine-selinux >= 17.05.0.ce-1.el7.centos,它被软件包 docker-engine-17.05.0.ce-1.el7.centos.x86_64 需要
--> 正在检查事务
---> 软件包 docker-engine-selinux.noarch.0.17.05.0.ce-1.el7.centos 将被 安装
--> 解决依赖关系完成
依赖关系解决
====================================================================
Package 架构 版本 源 大小
====================================================================正在安装:
docker-engine x86_64 17.05.0.ce-1.el7.centos dockerrepo 19 M
为依赖而安装:
docker-engine-selinux noarch 17.05.0.ce-1.el7.centos dockerrepo 28 k
事务概要
====================================================================
安装 1 软件包 (+1 依赖软件包)
总下载量:20 M
安装大小:70 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/dockerrepo/packages/docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID 2c52609d: NOKEY
docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm 的公钥尚未安装
(1/2): docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm | 28 kB 00:00:01
(2/2): docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm | 19 MB 00:00:09
--------------------------------------------------------------------
总计 2.0 MB/s | 20 MB 00:00:10
从 https://yum.dockerproject.org/gpg 检索密钥
导入 GPG key 0x2C52609D:
用户ID : "Docker Release Tool (releasedocker) <docker@docker.com>"
指纹 : 5811 8e89 f3a9 1289 7c07 0adb f762 2157 2c52 609d
来自 : https://yum.dockerproject.org/gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch 1/2
Re-declaration of type docker_t
Failed to create node
Bad type declaration at /etc/selinux/targeted/tmp/modules/400/docker/cil:1
/usr/sbin/semodule: Failed!
restorecon: lstat(/var/lib/docker) failed: No such file or directory
warning: %post(docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch) scriptlet failed, exit status 255
Non-fatal POSTIN scriptlet failure in rpm package docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch
正在安装 : docker-engine-17.05.0.ce-1.el7.centos.x86_64 2/2
验证中 : docker-engine-17.05.0.ce-1.el7.centos.x86_64 1/2
验证中 : docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch 2/2
已安装:
docker-engine.x86_64 0:17.05.0.ce-1.el7.centos
作为依赖被安装:
docker-engine-selinux.noarch 0:17.05.0.ce-1.el7.centos
启动Docker服务
启动Docker服务
[root@tar1 ~]# systemctl start docker.service
验证Docker是否已经启动
[root@tar1 ~]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2019-08-21 11:31:44 CST; 1min 5s ago
Docs: https://docs.docker.com
Main PID: 21109 (dockerd)
Tasks: 17
Memory: 58.9M
CGroup: /system.slice/docker.service
├─21109 /usr/bin/dockerd
└─21116 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 -...
8月 21 11:31:43 tar1 dockerd[21109]: time="2019-08-21T11:31:43.985321498+08:00" level=info msg="Loading containers: start."
8月 21 11:31:44 tar1 dockerd[21109]: time="2019-08-21T11:31:44.299547028+08:00" level=info msg="Default bridge (doc...dress"
8月 21 11:31:44 tar1 dockerd[21109]: time="2019-08-21T11:31:44.536150388+08:00" level=info msg="Loading containers: done."
8月 21 11:31:44 tar1 dockerd[21109]: time="2019-08-21T11:31:44.611884624+08:00" level=info msg="Daemon has complete...ation"
8月 21 11:31:44 tar1 dockerd[21109]: time="2019-08-21T11:31:44.611917359+08:00" level=info msg="Docker daemon" comm...5.0-ce
8月 21 11:31:44 tar1 dockerd[21109]: time="2019-08-21T11:31:44.626618666+08:00" level=info msg="API listen on /var/....sock"
8月 21 11:31:44 tar1 systemd[1]: Started Docker Application Container Engine.
8月 21 11:32:34 tar1 dockerd[21109]: time="2019-08-21T11:32:34.826419224+08:00" level=warning msg="Error getting v2...ders)"
8月 21 11:32:34 tar1 dockerd[21109]: time="2019-08-21T11:32:34.827115930+08:00" level=info msg="Attempting next end...ders)"
8月 21 11:32:34 tar1 dockerd[21109]: time="2019-08-21T11:32:34.827176510+08:00" level=error msg="Handler for POST /...ders)"
Hint: Some lines were ellipsized, use -l to show in full.
看到“running”字样,表示启动成功。
验证docker是否成功安装
[root@tar1 yum.repos.d]# docker run hello-world
WARNING: IPv4 forwarding is disabled. Networking will not work.
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.
(amd64)
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 it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
查看docker其他基本信息
查看版本信息
[root@tar1 ~]# docker version
Client:
Version: 17.05.0-ce
API version: 1.29
Go version: go1.7.5
Git commit: 89658be
Built: Thu May 4 22:06:25 2017
OS/Arch: linux/amd64
Server:
Version: 17.05.0-ce
API version: 1.29 (minimum version 1.12)
Go version: go1.7.5
Git commit: 89658be
Built: Thu May 4 22:06:25 2017
OS/Arch: linux/amd64
Experimental: false
查看其他信息
[root@tar1 ~]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.05.0-ce
Storage Driver: overlay
Backing Filesystem: xfs
Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9048e5e50717ea4497b757314bad98ea3763c145
runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 991.2MiB
Name: tar1
ID: BBHS:KT7X:XGDO:ZRYJ:O6CH:GVW5:DLQR:TLRJ:JOCV:2XXH:Y5K7:MTBO
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Docker的启动和关闭
docker stop pg11
docker start pg11
pg11是docker的名字。
总结
一开始可能很难理解容器化相关概念,您只需尝试安装使用,概念便逐渐清楚了。

http://xinguang.sctcbmw.cn/xicang/ http://shenghuo.china-bbs.com/bayinguoleng/ http://gx.lztcxxw.cn/esmbc/ http://gx.lztcxxw.cn/cszp/ https://huairou.tiancebbs.cn/ https://andunzhen.tiancebbs.cn/ https://pan.tiancebbs.cn/ http://ouyu.hftcbmw.cn/sbzl/ https://kanghezhen.tiancebbs.cn/ http://huaguang.jxtcbmw.cn/gren/ https://hegang.tiancebbs.cn/ http://tuiguang.hntcxxw.cn/bayinguoleng/ http://nalei.zjtcbmw.cn/cqna/ http://shenghuo.china-bbs.com/haikou/ http://yz.cqtcxxw.cn/hnld/ http://ouyu.hftcbmw.cn/hbez/ https://dongming.tiancebbs.cn/
猪网名:https://www.nanss.com/mingcheng/5859.html 劳动最光荣的名言佳句:https://www.nanss.com/xuexi/5103.html 从一到十的成语祝福语:https://www.nanss.com/yulu/5330.html 搞笑又沙雕的情侣id:https://www.nanss.com/mingcheng/5640.html 一句话介绍自己:https://www.nanss.com/xuexi/5549.html 关于教育的唯美句子:https://www.nanss.com/xuexi/5930.html 有关星辰的好听的昵称:https://www.nanss.com/mingcheng/5820.html 六年级上册变形记作文400字:https://www.nanss.com/xuexi/5273.html 唯美风景最好的网名:https://www.nanss.com/mingcheng/5679.html 宝藏女孩憨憨类似网名:https://www.nanss.com/mingcheng/5721.html 乐此不疲造句:https://www.nanss.com/xuexi/5764.html 最美茶语:https://www.nanss.com/yulu/5698.html 王者荣耀最撩人的id:https://www.nanss.com/mingcheng/5674.html 带马字的网名:https://www.nanss.com/mingcheng/5664.html 丧气网名:https://www.nanss.com/mingcheng/5638.html 魔兽世界战士名字大全:https://www.nanss.com/mingcheng/5877.html 作文中惊艳的神仙开头:https://www.nanss.com/xuexi/5789.html 妈妈生病难过心情短语:https://www.nanss.com/wenan/5843.html 孩子得奖状了发朋友圈怎么说:https://www.nanss.com/wenan/5688.html 赞美领导的经典语句:https://www.nanss.com/gongzuo/5925.html 晒吃榴莲的经典说说:https://www.nanss.com/wenan/5614.html 搞笑又高端的朋友圈:https://www.nanss.com/wenan/5708.html 发个库存的幽默说说:https://www.nanss.com/wenan/5663.html 十岁成长礼文案:https://www.nanss.com/wenan/5985.html 女生沙雕网名:https://www.nanss.com/mingcheng/5639.html 十年磨一剑下一句:https://www.nanss.com/xuexi/5402.html 感谢你曾经来过:https://www.nanss.com/yuedu/5553.html 六年级毕业感谢老师的话:https://www.nanss.com/xuexi/5139.html 撩媳妇的情话:https://www.nanss.com/yulu/5691.html 七夕失望的心情说说:https://www.nanss.com/wenan/5818.html