案例说明:
1)CentOS 7.6的基础镜像上创建PostgreSQL 11.6镜像。
2)创建PostgreSQL 11.6 镜像的Dockerfile。
3)下载PostgreSQL 11.6的源码包和Dockerfile存放在相同的目录下
1、启动docker服务
[root@node1 soft]# systemctl start docker
[root@node1 soft]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2021-03-01 12:53:36 CST; 5s ago
Docs: http://docs.docker.com
Main PID: 30805 (dockerd-current)
CGroup: /system.slice/docker.service
├─30805 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current -...
└─30812 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd...
Mar 01 12:53:34 node1 dockerd-current[30805]: time="2021-03-01T12:53:34.468625402+08:00" level=warning msg=...ed."
Mar 01 12:53:34 node1 dockerd-current[30805]: time="2021-03-01T12:53:34.468793842+08:00" level=info msg="Lo...rt."
Mar 01 12:53:34 node1 dockerd-current[30805]: time="2021-03-01T12:53:34.844387027+08:00" level=info msg="Fi...lse"
Mar 01 12:53:35 node1 dockerd-current[30805]: time="2021-03-01T12:53:35.360425213+08:00" level=info msg="De...ess"
Mar 01 12:53:35 node1 dockerd-current[30805]: time="2021-03-01T12:53:35.927237269+08:00" level=info msg="Lo...ne."
Mar 01 12:53:35 node1 dockerd-current[30805]: time="2021-03-01T12:53:35.930691458+08:00" level=warning msg=...fix"
Mar 01 12:53:36 node1 dockerd-current[30805]: time="2021-03-01T12:53:36.131001989+08:00" level=info msg="Da...ion"
Mar 01 12:53:36 node1 dockerd-current[30805]: time="2021-03-01T12:53:36.131032779+08:00" level=info msg="Do...13.1
Mar 01 12:53:36 node1 systemd[1]: Started Docker Application Container Engine.
Mar 01 12:53:36 node1 dockerd-current[30805]: time="2021-03-01T12:53:36.139978518+08:00" level=info msg="AP...ock"
Hint: Some lines were ellipsized, use -l to show in full.
2、查看系统网络
[root@node1 soft]# ip add sh
......
6: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:4c:1e:ed:58 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
3、查看dockerfile
[root@node1 soft]# cat Dockerfile
FROM centos:centos7.6.1810
ENV PGHOME usr/local/pgsql-11.6 PGDATA pgdata/data
RUN yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake make gettext gettext-devel
RUN useradd postgres && mkdir -p pgdata/data && chown postgres.postgres -R pgdata/
ADD postgresql-11.6.tar.gz postgresql
RUN postgresql/postgresql-11.6/configure --prefix=/usr/local/pgsql-11.6 --with-segsize=16 --with-wal-segsize=512 --with-blocksize=32 --with-wal-blocksize=64 --with-libxslt --enable-thread-safety --with-pgport=5432 --with-libedit-preferred --with-perl --with-openssl --with-libxml --with-libxslt --enable-thread-safety --enable-nls=en_US.UTF-8 && make && make install
USER postgres
ENV PGHOME=/usr/local/pgsql-11.6
ENV PGDATA=/pgdata/data
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib
ENV PATH=$PATH:$PGHOME/bin/
RUN /usr/local/pgsql-11.6/bin/initdb -D /pgdata/data && /usr/local/pgsql-11.6/bin/pg_ctl -D /pgdata/data -l /pgdata/data/logfile start
# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all all 0.0.0.0/0 md5" >> /pgdata/data/pg_hba.conf
# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
RUN echo "listen_addresses=‘*‘" >> /pgdata/data/postgresql.conf
# Expose the PostgreSQL port
EXPOSE 5432
WORKDIR /home/postgres
CMD ["/usr/local/pgsql-11.6/bin/postgres","-D","/pgdata/data"]
4、创建自定义postgresql镜像
[root@node1 data1]# ls -lh
total 25M
-rw-r--r-- 1 root root 1.6K Mar 1 12:04 Dockerfile
-rw-r--r-- 1 root root 25M Mar 1 12:20 postgresql-11.6.tar.gz
drwxr-xr-x 2 root root 4.0K Mar 1 12:20 soft
=创建postgresql镜像=
[root@node1 data1]# docker build -t postgresql:11.6-centos7.6 .
Sending build context to Docker daemon 6.599 GB
Step 1/17 : FROM centos:centos7.6.1810
Trying to pull repository docker.io/library/centos ...
centos7.6.1810: Pulling from docker.io/library/centos
ac9208207ada: Pull complete
Digest: sha256:62d9e1c2daa91166139b51577fe4f4f6b4cc41a3a2c7fc36bd895e2a17a3e4e6
Status: Downloaded newer image for docker.io/centos:centos7.6.1810
---> f1cb7c7d58b7
Step 2/17 : ENV PGHOME usr/local/pgsql-11.6 PGDATA pgdata/data
---> Running in 780afc3fc74b
---> 98cdef1a52b9
Removing intermediate container 780afc3fc74b
Step 3/17 : RUN yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake make gettext gettext-devel
---> Running in 664e1b2672a3
###依赖包安装###
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.huaweicloud.com
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package cmake.x86_64 0:2.8.12.2-2.el7 will be installed
136/140
......
Verifying : 1:openssl-libs-1.0.2k-16.el7.x86_64 137/140
Verifying : glibc-common-2.17-260.el7.x86_64 138/140
Verifying : libxml2-python-2.9.1-6.el7_2.3.x86_64 139/140
Verifying : libselinux-2.5-14.1.el7.x86_64 140/140
Installed:
......
python-devel.x86_64 0:2.7.5-90.el7
readline-devel.x86_64 0:6.2-11.el7
zlib-devel.x86_64 0:1.2.7-19.el7_9
Dependency Installed:
bc.x86_64 0:1.06.95-13.el7
cpp.x86_64 0:4.8.5-44.el7
cyrus-sasl.x86_64 0:2.1.26-23.el7
......
Dependency Updated:
glibc.x86_64 0:2.17-324.el7_9 glibc-common.x86_64 0:2.17-324.el7_9
......
Complete!
---> 4d1f38e45da8
Removing intermediate container 664e1b2672a3
Step 4/17 : RUN useradd postgres && mkdir -p pgdata/data && chown postgres.postgres -R pgdata/
---> Running in 0fceed8cf6ed
---> 54cfd5c983e9
Removing intermediate container 0fceed8cf6ed
Step 5/17 : ADD postgresql-11.6.tar.gz postgresql
---> 3195cd7612fa
Removing intermediate container c72392ce2c20
Step 6/17 : RUN postgresql/postgresql-11.6/configure --prefix=/usr/local/pgsql-11.6 --with-segsize=16 --with-wal-segsize=512 --with-blocksize=32 --with-wal-blocksize=64 --with-libxslt --enable-thread-safety --with-pgport=5432 --with-libedit-preferred --with-perl --with-openssl --with-libxml --with-libxslt --enable-thread-safety --enable-nls=en_US.UTF-8 && make && make install
---> Running in 6b0cb0e8186e
### 编译安装postgresql###
configure: WARNING: unrecognized options: --with-wal-segsize
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... yes
......
make -C config install
make[1]: Entering directory `/config‘
/usr/bin/mkdir -p ‘/usr/local/pgsql-11.6/lib/pgxs/config‘
/usr/bin/install -c -m 755 //postgresql/postgresql-11.6/config/install-sh ‘/usr/local/pgsql-11.6/lib/pgxs/config/install-sh‘
/usr/bin/install -c -m 755 //postgresql/postgresql-11.6/config/missing ‘/usr/local/pgsql-11.6/lib/pgxs/config/missing‘
make[1]: Leaving directory `/config‘
PostgreSQL installation complete.
---> e9fb25e59ef7
Removing intermediate container 6b0cb0e8186e
Step 7/17 : USER postgres
---> Running in d0680aa56ff0
---> b4abc4eca46e
Removing intermediate container d0680aa56ff0
Step 8/17 : ENV PGHOME /usr/local/pgsql-11.6
---> Running in 935b9756e459
---> 45cdc9be6dc4
Removing intermediate container 935b9756e459
Step 9/17 : ENV PGDATA /pgdata/data
---> Running in 94c1359ec411
---> 8d119ae74816
Removing intermediate container 94c1359ec411
Step 10/17 : ENV LD_LIBRARY_PATH $LD_LIBRARY_PATH:$PGHOME/lib
---> Running in 6cf4be6baed7
---> 135f00734909
Removing intermediate container 6cf4be6baed7
Step 11/17 : ENV PATH $PATH:$PGHOME/bin/
---> Running in f610fe587ccc
---> b8558ddbdc41
Removing intermediate container f610fe587ccc
Step 12/17 : RUN /usr/local/pgsql-11.6/bin/initdb -D /pgdata/data && /usr/local/pgsql-11.6/bin/pg_ctl -D /pgdata/data -l /pgdata/data/logfile start
---> Running in 0fb734c6783a
The 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 "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /pgdata/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ...
WARNING: enabling "trust" authentication for local connections
You 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.
ok
Success. You can now start the database server using:
/usr/local/pgsql-11.6/bin/pg_ctl -D /pgdata/data -l logfile start
waiting for server to start.... done
server started
---> 7ab587480d28
Removing intermediate container 0fb734c6783a
Step 13/17 : RUN echo "host all all 0.0.0.0/0 md5" >> /pgdata/data/pg_hba.conf
---> Running in 75cf5f3aa929
---> ec5dd7c04aea
Removing intermediate container 75cf5f3aa929
Step 14/17 : RUN echo "listen_addresses=‘*‘" >> /pgdata/data/postgresql.conf
---> Running in b4cbd912567f
---> 488900039f30
Removing intermediate container b4cbd912567f
Step 15/17 : EXPOSE 5432
---> Running in 7d587e24e008
---> 0fc240554f0a
Removing intermediate container 7d587e24e008
Step 16/17 : WORKDIR /home/postgres
---> d79a011a0685
Removing intermediate container 2925a0267eb5
Step 17/17 : CMD /usr/local/pgsql-11.6/bin/postgres -D /pgdata/data
---> Running in 03c44a097cf4
---> 2632ccf24230
Removing intermediate container 03c44a097cf4
Successfully built 2632ccf24230
5、查看当前系统下的image
[root@node1 data1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
postgresql 11.6-centos7.6 2632ccf24230 5 minutes ago 821 MB
docker.io/postgres 10 0596e5d538ec 7 weeks ago 200 MB
docker.io/docker latest 08bdaf2f88f9 2 months ago 246 MB
docker.io/dbmses/kingbase latest 30ec9e652bc8 5 months ago 486 MB
docker.io/centos centos7.6.1810 f1cb7c7d58b7 2 years ago 202 MB
6、启动和运行container
[root@node1 data1]# docker run -it -d --name pg1 postgresql:11.6-centos7.6
c51817f42cf5a04b0843c7a48e8e311b2aa4ab7fd0a5e159f98951b6c44dad1b
7、查看当前运行的container
[root@node1 data1]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c51817f42cf5 postgresql:11.6-centos7.6 "/usr/local/pgsql-..." 28 seconds ago Up 27 seconds 5432/tcp pg1
8、连接访问postgresql container
[root@node1 data1]# docker exec -it pg1 /bin/bash
[postgres@c51817f42cf5 ~]$ ps -ef |grep postgre
postgres 1 0 0 09:03 ? 00:00:00 /usr/local/pgsql-11.6/bin/postgres -D /pgdata/data
postgres 6 1 0 09:03 ? 00:00:00 postgres: startup
[postgres@c51817f42cf5 ~]$ psql
psql (11.6)
Type "help" for help.
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 11.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+-----------+---------+-------+-----------------------
postgres | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# create database prod;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+-----------+---------+-------+-----------------------
postgres | postgres | SQL_ASCII | C | C |
prod | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c prod
You are now connected to database "prod" as user "postgres".
prod=# create table t1 (id int);
CREATE TABLE
prod=# insert into t1 values (10),(20),(30);
INSERT 0 3
prod=# select * from t1;
id
----
10
20
30
(3 rows)
9、关闭postgresql container
1)查看当前的container
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c51817f42cf5 postgresql:11.6-centos7.6 "/usr/local/pgsql-..." 25 minutes ago Up 25 minutes 5432/tcp pg1
2)关闭postgresql container
[root@node1 ~]# docker stop c51817f42cf5
c51817f42cf5
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
参考文档:https://www.modb.pro/db/15084(感谢原作)
构建基于CentOS 7.6 的PostgreSQL 11.6 镜像
原文:https://www.cnblogs.com/tiany1224/p/15134303.html