1.查询postgresql镜像 docker search postgres,结果如下:
$ sudo docker search postgre
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
postgres The PostgreSQL object-relational database sy... 12992 [OK]
bitnami/postgresql Bitnami PostgreSQL Docker Image 255 [OK]
cimg/postgres 1
bitnami/postgres-exporter 12
bitnami/postgresql-repmgr 23
ubuntu/postgres PostgreSQL is an open source object-relation... 32
rapidfort/postgresql RapidFort optimized, hardened image for Post... 22
rapidfort/postgresql-official RapidFort optimized, hardened image for Post... 12
bitnamicharts/postgresql 2
rapidfort/postgresql12-ib RapidFort optimized, hardened image for Post... 11
参数解释:
NAME --镜像名称
DESCRIPTION --镜像描述
STARS --标星数
OFFICIAL --官方的
AUTOMATED -- 自动化
第一个为postgres官方镜像文件!我们就拉取第一个镜像。
2.拉取镜像文件 docker pull postgres
$ sudo docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
2f44b7a888fa: Pull complete
6d49150dabe2: Pull complete
...
Digest: sha256:49c276fa02e3d61bd9b8db81dfb4784fe814f50f778dce5980a03817438293e3
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest
3.镜像文件拉取成功l , 用 docker images看看
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres latest 75b7bff7c3ad 13 days ago 425MB
4.创建挂载文件夹
$ sudo mkdir postgresql
在固定的目录创建挂载文件夹(以后所有镜像的挂载目录都在固定目录,方便后期维护)
5.启动docker 镜像
$ sudo docker run --name postgres --restart=always -e POSTGRES_PASSWORD=Shn123 -p 5432:5432 -v /stool/postgresql:/var/lib/postgresql/data -d postgres:latest
2526e07723ec27bab8670c869900764dc3d02c888344c2b38c006fae79c6c7ab
run: 创建并运行一个容器;
--restart=always 表示容器退出时,docker会总是自动重启这个容器;
--name: 指定创建的容器的名字;
-e POSTGRES_PASSWORD=Shn123: 设置环境变量,指定数据库的登录口令为Shn123;
-p 5432:5432: 端口映射将容器的5432端口映射到外部机器的5432端口;
-v /stool/postgresql:/var/lib/postgresql/data 将运行镜像的/var/lib/postgresql/data目录挂载到宿主机/stool/postgresql目录
-d postgres:latest: 指定使用postgres:latest作为镜像。
6.查看启动日志 docker logs postgres,由于日志过多,这里仅查看5行,要是想全查,就不要带 -t -f --tail 5
$ sudo docker logs postgres -t -f --tail 5
2024-01-18T07:29:54.405797974Z 2024-01-18 07:29:54.405 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2024-01-18T07:29:54.405831116Z 2024-01-18 07:29:54.405 UTC [1] LOG: listening on IPv6 address "::", port 5432
2024-01-18T07:29:54.412304129Z 2024-01-18 07:29:54.412 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-01-18T07:29:54.420699699Z 2024-01-18 07:29:54.420 UTC [63] LOG: database system was shut down at 2024-01-18 07:29:54 UTC
2024-01-18T07:29:54.428606162Z 2024-01-18 07:29:54.428 UTC [1] LOG: database system is ready to accept connections
7.查看运行的容器,看看镜像是否启动成功 docker ps
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2526e07723ec postgres:latest "docker-entrypoint.s..." 6 minutes ago Up 6 minutes 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp postgres
8.本地连接测试数据库是否连接成功
#方法一,进入docker查看
$ sudo docker exec -it postgres bash
root@2526e07723ec:/# psql -d postgres -u postgres
#方法二,可以用自己的连接工具查看
到这里就安装成功啦~~~ 自己玩去吧~