
上篇介绍容器相关的基本概念,介绍如何安装 docker。如果不熟悉 docker 的安装,可回到系列文章的第一部分《Docker与PostgreSQL 11.5系列文章(一)Docker的安装》,第二部分主要介绍 PostgreSQL11 在容器中的安装和使用。
下载镜像
这里下载的 postgreSQL 11.5 版本的镜像
[root@tar1 yum.repos.d]# docker pull postgres:11.511.5: Pulling from library/postgres9fc222b64b0a: Pull complete 38296355136d: Pull complete 2809e135bbdb: Pull complete 77907400401d: Pull complete 1f8ceea7d21e: Pull complete 1ede1f2cb1b8: Pull complete dcfbc2751885: Pull complete 8379f81b7622: Pull complete 08034f3aaedc: Pull complete cb6db97b313d: Pull complete 7a41d1ed6f3b: Pull complete 15f5a1254505: Pull complete 5a9451e12286: Pull complete 55bb5e2d55ba: Pull complete Digest: sha256:f766d03bb49c7dd16fe32c1eb2645d13cb315adc029557cd1e4a0a9c094933d5Status: Downloaded newer image for postgres:11.5
下载要花费一定的时间。
查看镜像
[root@tar1 yum.repos.d]# docker images |grep postpostgres            11.5                c3fe76fef0a6        9 days ago          312MB[root@tar1 yum.repos.d]#
下载成功以后,可以查看镜像的信息(包括大小)。
创建和运行容器
[root@tar1 yum.repos.d]# docker run --name dockerPG11 -e POSTGRES_PASSWORD=Xzzp2008 -p 54322:5432 -d postgres:11.5ccb3d0bb1a303e081fac20622c0b821b71720308280a8d1a55b906e792fe6452
- run 创建并运行一个容器(实例); 
- –name 指定创建的容器的名字; 
- -e POSTGRES_PASSWORD= Xzzp2008 设置环境变量,指定数据库的登录口令为 Xzzp2008; 
- -p 54322:5432 端口映射将容器的 5432 端口映射到外部机器的 54322 端口,从外部访问就使用这个端口; 
- -d postgres:11.5 镜像的名字是 postgres:11.5,镜像的名字是查询出来的,不是由用户随意定的。 
查看实例
[root@tar1 yum.repos.d]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMESccb3d0bb1a30        postgres:11.5       "docker-entrypoint..."   37 seconds ago      Up 36 seconds       5432/tcp, 0.0.0.0:54322->54333/tcp   dockerPG11
可以看到实例运行的时间,ID 号等信息。
多了参数-a ,是不一样的输出
[root@tar1 yum.repos.d]# docker ps -aCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                                NAMESccb3d0bb1a30        postgres:11.5       "docker-entrypoint..."   14 minutes ago      Up 14 minutes                  5432/tcp, 0.0.0.0:54322->54333/tcp   dockerPG113b8d602037b4        hello-world         "/hello"                 About an hour ago   Exited (0) About an hour ago                                        jolly_wrightc5cdfc83f7bd        hello-world         "/hello"                 About an hour ago   Exited (0) About an hour ago                                        elegant_edison
查看日志
[root@tar1yum.repos.d]# docker logs ccb3d0bb1a303e081fac20622c0b821b71720308280a8d1a55b906e792fe6452The files belonging to this database system will be owned by user "postgres".This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default timezone ... Etc/UTCselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... ok
Success. You can now start the database server using:
    pg_ctl -D /var/lib/postgresql/data -l logfile start
WARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.waiting for server to start....2019-08-23 09:48:02.765 UTC [39] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"2019-08-23 09:48:02.791 UTC [40] LOG:  database system was shut down at 2019-08-23 09:48:01 UTC2019-08-23 09:48:02.797 UTC [39] LOG:  database system is ready to accept connections doneserver started
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
waiting for server to shut down....2019-08-23 09:48:02.846 UTC [39] LOG:  received fast shutdown request2019-08-23 09:48:02.849 UTC [39] LOG:  aborting any active transactions2019-08-23 09:48:02.852 UTC [39] LOG:  background worker "logical replication launcher" (PID 46) exited with exit code 12019-08-23 09:48:02.852 UTC [41] LOG:  shutting down2019-08-23 09:48:02.881 UTC [39] LOG:  database system is shut down doneserver stopped
PostgreSQL init process complete; ready for start up.
2019-08-23 09:48:02.967 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 54322019-08-23 09:48:02.967 UTC [1] LOG:  listening on IPv6 address "::", port 54322019-08-23 09:48:02.973 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"2019-08-23 09:48:02.989 UTC [48] LOG:  database system was shut down at 2019-08-23 09:48:02 UTC2019-08-23 09:48:02.996 UTC [1] LOG:  database system is ready to accept connections
进入容器里面(进入容器内部)
运行中的容器其实是一个功能完备的 Linux 操作系统,所以我们可以像常规的系统一样登录并访问容器。
[root@tar1 yum.repos.d]# docker exec -it dockerPG11 bashroot@ccb3d0bb1a30:/#
dockerPG11 是容器的名字。
ccb3d0bb1a30 是容器的 ID 号。
查看 PG 进程(在容器里)
root@ccb3d0bb1a30:/# ps -ef |grep postpostgres     1     0  0 09:47 ?        00:00:00 postgrespostgres    49     1  0 09:48 ?        00:00:00 postgres: checkpointer  postgres    50     1  0 09:48 ?        00:00:00 postgres: background writer  postgres    51     1  0 09:48 ?        00:00:00 postgres: walwriter  postgres    52     1  0 09:48 ?        00:00:00 postgres: autovacuum launcher  postgres    53     1  0 09:48 ?        00:00:00 postgres: stats collector  postgres    54     1  0 09:48 ?        00:00:00 postgres: logical replication launcher  root        69    55  0 10:10 ?        00:00:00 grep post
注意:看到 ID 号 ccb3d0bb1a30,你就知道,当前在容器内部。
切换到 OS 用户 postgres(在容器里)
root@ccb3d0bb1a30:/# su - postgrespostgres@ccb3d0bb1a30:~$ postgres@ccb3d0bb1a30:~$
登录数据库(在容器里)
postgres@ccb3d0bb1a30:~$ psql -d postgrespsql (11.5 (Debian 11.5-1.pgdg90+1))Type "help" for help.
postgres=#
查看映射到 docker 外的端口
[root@tar1 ~]# netstat -ntlActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         State      tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     tcp6       0      0 ::1:25                  :::*                    LISTEN     tcp6       0      0 :::111                  :::*                    LISTEN     tcp6       0      0 :::54322                :::*                    LISTEN     tcp6       0      0 :::22                   :::*                    LISTEN     tcp6       0      0 ::1:631
54321 就是访问容器内 postgres 数据库的端口号。
登录数据库(从容器外部)
从宿主机登录容器数据库
-bash-4.2$ psql -U postgres -h localhost -p 54322用户 postgres 的口令:psql (11.5)输入 "help" 来获取帮助信息.
postgres=#
查看版本号
postgres=# select version();                                                             version                                                              ---------------------------------------------------------------------------------- PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit(1 行记录)
postgres=#
54322 是映射以后的密码,
DML 测试
创建表,插入数据,查询数据等一系列实验:
postgres=# CREATE TABLE weather (postgres(# city varchar(80),postgres(# temp_lo int, -- low temperaturepostgres(# temp_hi int, -- high temperaturepostgres(# prcp real, -- precipitationpostgres(# date datepostgres(# );CREATE TABLEpostgres=# INSERT INTO weather VALUES ('San Francisco',46,50,0.25, '1994-11-27');INSERT 0 1postgres=# INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');INSERT 0 1postgres=# INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54, 37);INSERT 0 1postgres=# SELECT * FROM weather;     city      | temp_lo | temp_hi | prcp |    date    ---------------+---------+---------+------+------------ San Francisco |      46 |      50 | 0.25 | 1994-11-27 San Francisco |      43 |      57 |    0 | 1994-11-29 Hayward       |      37 |      54 |      | 1994-11-29(3 行记录)
postgres=#
postgres=# \dt                关联列表 架构模式 |  名称   |  类型  |  拥有者  ----------+---------+--------+---------- public   | weather | 数据表 | postgres(1 行记录)
停止实例(容器)
docker stop ccb3d0bb1a30ccb3d0bb1a30是实例(容器)的ID或者(dockerPG11是容器的名字)docker stop dockerPG11
其实也是停止 PG11.5。
删除容器
[root@tar1 ~]# docker rm dockerPG11dockerPG11
dockerPG11 是容器的名字
删除容器以后,可以把容器所引用的卷给删除
[root@tar1 ~]# ll /var/lib/docker/volumes/总用量 32drwxr-xr-x. 3 root root    19 8月  23 23:46 cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f-rw-------. 1 root root 65536 8月  25 12:13 metadata.db[root@tar1 ~]# docker volume rm cf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6fcf103a7ead15ff3e25f42e83703f06544cfbf018fe469d577002dff9f692bb6f[root@tar1 ~]#  ll /var/lib/docker/volumes/总用量 32-rw-------. 1 root root 65536 8月  25 12:32 metadata.db[root@tar1 ~]#
总结
在这篇文章的学习中,需注意端口是如何映射的,还要特别注意在容器里面和容器外面看到的情况。同时注意: 如何从容器外登录数据库?如何从容器里登录数据库?停止 docker 服务和停止 docker 的区别。
作者介绍:
赵振平,PostgreSQL 中文社区主席、计算机畅销书作家、贵州省省管专家、太阳塔科技 CTO、国家首批大数据高级职称、腾讯最具价值专家(TVP)、电子工业出版社签约作家、出版了技术专著《Oracle 数据库精讲与疑难解析》、出版了技术专著《成功之路:Oracle 11g 学习笔记》、出版了技术专著《IT 架构实录》。
更多内容推荐
- 卧槽,误删数据库了,会被开除吗?- 无论是开发、测试,还是DBA,都难免会涉及到数据库的操作,比如:创建某张表,添加某个字段、添加数据、更新数据、删除数据、查询数据等等。 - 2021 年 4 月 14 日 
- 21 道最新 Java 面试题剖析(数据库 +JVM+ 微服务 + 高并发)- 面试题解析 - 2021 年 7 月 28 日 
- 沙场秋点兵——MySQL 容器化性能测试对比- 容器技术改变了应用交付、运行的方式,几乎各种Linux环境下的应用程序都可以使用容器来运行。但是否能在容器环境里运行数据库应用,以及数据库应用是否适合在容器里运行,一直都是大家很关注的问题,今天我们就来深入分析一下容器环境运行MySQL数据库的事。 - 2021 年 8 月 5 日 
- 37 丨 SQL 注入:你的 SQL 是如何被注入的?- 我们能经常看到用户信息被泄露,这种情况很大程度上和SQL注入有关,了解SQL注入的原理以及防范是非常有必要的。 - 2019 年 9 月 18 日 
- 在容器中使用 TensorFlow- 2019 年 1 月 7 日 
- 想清楚这 10 点再部署 Kubernetes 也不迟- 本文总结了十大技术专家关于容器和Kubernetes的部署提示。 
- 滴滴基于大数据的用户问题定位建设与实践- 2018 年 9 月 12 日 
- 基于腾讯云 EKS 的容器化部署实战- 今天,我来手把手教你如何在Kubernetes集群中部署好IAM应用。因为步骤比较多,所以希望你能跟着我完成每一个操作步骤。 - 2021 年 9 月 16 日 
- Docker 与 PostgreSQL 11.5 系列文章(三):数据持久化- 本文主要讨论容器的持久化。“持久化” 简单理解,就是容器被关闭后PostgreSQL数据库的数据是否还存在? 
- 结课测试 | “即时消息技术剖析与实战”100 分试卷等你来挑战!- 还等什么,快来测测你的IM知识水平吧! - 2020 年 4 月 23 日 
- PostgreSQL 高可用:多主复制解决方案- 作者:伊布拉尔·艾哈迈德(Ibrar Ahmed)在2018年7月加入Percona。在加入Percona之前,Ibrar曾在EnterpriseDB担任高级数据库架构师10年。Ibrar拥有18年的软件开发经验。Ibrar在PostgreSQL上写了多本书。 - 2020 年 12 月 18 日 
- 云原生技术采用增加,全球 60% 后端开发人员都在使用容器 | 趋势分享- 导读:过去几年里,随着数字经济的发展,数字化转型成为企业发展的核心战略。企业上云已是大势所趋,以容器为基础的云原生理念与技术应运而生,并被用户广泛接受。基于容器、微服务、DevOps、服务网格等新型云原生技术,正在深刻推动着企业IT变革实现全面数字 - 2020 年 8 月 28 日 
- 【总结】性能优化 2- 数据结构和算法、网络通讯、数据库 - 2020 年 7 月 26 日 
- Docker 与 PostgreSQL 11.5 系列文章(一):Docker 的安装- Docker前几年席卷了整个互联网,Docker在互联网很多业务场景都有使用。最近一两年,一些传统行业也在尝试使用,并且看到不少案例。另外,我们一直担心,容器影响数据库的性能,根据近些年的亲身实践,容器对数据库性能的影响很小,也不会影响数据库的并行性。因此,想写一系列文章,简单介绍PostgreSQL在容器中的实践。 
- 一个内核漏洞详解:容器逃逸- 该漏洞是由Andy Nguyen (theflow@)发现,于2021年07月16日发布。换句话说,该漏洞已经存在了15年之久,都没有被人发现。该漏洞将允许本地用户通过用户名空间获取权限提升,和容器逃逸。 - 2021 年 8 月 31 日 
- 阿里搜索中台在 DevOps 和 AIOps 的思考及实践- 2018 年 12 月 18 日 
- Longhorn 入门级教程!轻松实现持久化储存!- 本文来自RancherLabs微信公众号 
- 资深专家都知道的 Docker 常用命令- 目前,Docker共有13个管理命令和41个通用命令,本文是常用Docker命令列表。 
推荐阅读
- 初学 Docker 容器网络不得不看的学习笔记
- 环境准备 | 带你安装 MySQL 和图形化管理工具 Workbench- 2021 年 3 月 8 日 
- SQL Server 容器介绍
- 程序员练级攻略:容器化和自动化运维- 2018 年 7 月 24 日 
- Powerset:HBase 的老东家- 2018 年 4 月 27 日 
- 【入门必读】《TcaplusDB 数据库常见问题解决及诊断技巧集锦 - 数据库使用类 -2》- 2021 年 7 月 28 日 
- 从 Docker、Twistlock、CoreOS 看容器安全的现状
电子书

大厂实战PPT下载
换一换 
赵中州 | 阿里巴巴 达摩院高级算法专家
宋松涛 | 中原银行 信息技术部系统开发工程师
池建强 | 极客邦科技 总裁










 
    
评论