Docker安装部署Postgresql

  • Post author:
  • Post category:其他


环境:

Centos7.x + docker1.13.1 + postgres12

本文使用的是在镜像仓库直接pull的方式,非Dockfile的方式。

Postgresql的Dockerfile参考:

https://github.com/docker-library/postgres/blob/master/12/alpine/Dockerfile

常用命令

docker命令大全:

https://www.runoob.com/docker/docker-command-manual.html


请注意区分“镜像”和“容器”的。先创建镜像,然后通过镜像创建容器,一个镜像可以有多个容器。

镜像常用命令

命令

作用

docker image list

查询镜像列表

docker rmi -f 镜像ID

通过镜像ID删除

容器常用命令

docker run

容器运行

docker start/stop/restart

启动/停止/重启

docker rm 容器ID

删除容器

docker ps

容器查询,-a -q

docker exec -it 容器ID /bin/sh

进入容器命令行

docker volume create xxx

创建容器共享数据卷

docker volume inspect xxx

容器共享数据卷落盘

1.查询docker镜像

# docker search postgres

2.下载docker镜像

# docker pull postgres:12.7

下载后,镜像查询,# docker image list

3.创建本地卷

数据卷可以再容器之间共享

docker volume create PGDATA
docker volume inspect PGDATA
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/PGDATA/_data",
        "Name": "PGDATA",
        "Options": {},
        "Scope": "local"
    }
]

Mountpoint属性路径就是将来容器的data目录。

4.运行容器

docker run --name postgres12 -e POSTGRES_PASSWORD=postgres -p 5433:5432 -v PGDATA:/var/lib/postgresql/data -d postgres:12.7
  1. run:创建并运行一个容器

  1. –name:容器名称

  1. -e POSTGRES_PASSWORD:数据库密码

  1. -p:映射端口。前面端口是对外端口,后面端口是容器内使用端口。

  1. -v:PGDATA挂载到容器的/var/lib/postgresql/data (因为pg的Dockerfile声明的目录)

  1. -d postgres:12.7:使用image镜像的名称

返回一串字符,就是启动的容器ID。

容器ID:b8e40f13e2f3

镜像名称:postgres:12.7

对外暴露端口:5433

容器名称:postgres12

pg数据库默认密码:postgres

5.查看日志

# docker logs b8e40f13e2f3

[root@sougou opt]# docker logs b8e40f13e2f3
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 "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
...
2023-01-31 07:29:59.706 UTC [1] LOG:  starting PostgreSQL 12.7 (Debian 12.7-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2023-01-31 07:29:59.707 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-01-31 07:29:59.707 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-01-31 07:29:59.713 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-01-31 07:29:59.730 UTC [63] LOG:  database system was shut down at 2023-01-31 07:29:59 UTC
2023-01-31 07:29:59.734 UTC [1] LOG:  database system is ready to accept connections

6.进入容器使用psql

# docker exec -it b8e40f13e2f3 /bin/sh

root@sougou opt]# docker exec -it b8e40f13e2f3 /bin/sh
# psql -U postgres -p 5432
psql (12.7 (Debian 12.7-1.pgdg100+1))
Type "help" for help.

postgres=# select version();
                                                     version                                                      
------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.7 (Debian 12.7-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
(1 row)
# exit

7.使用数据库管理工具连接

到此结束。



版权声明:本文为w345731923原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。