docker部署Postgresql数据库

一、查看本地环境

1.1查看centos信息

[root@centos ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

1.2 检查docker版本

[root@centos ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.18
 API version:       1.41
 Go version:        go1.18.6
 Git commit:        b40c2f6
 Built:             Thu Sep  8 23:14:08 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.18
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.6
  Git commit:       e42327a
  Built:            Thu Sep  8 23:12:21 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

1.3 查看docker运行状态

[root@centos ~]# 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 Sun 2024-02-18 15:57:43 CST; 2 months 28 days ago
     Docs: https://docs.docker.com
 Main PID: 21564 (dockerd)
    Tasks: 22
   Memory: 556.8M

二、下载Postgresql镜像

[root@centos ~]# docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
09f376ebb190: Pull complete 
119215dfb3e3: Pull complete 
e02bbc8c8252: Pull complete 
061f31803c55: Pull complete 
accd4903f49a: Pull complete 
2016ff8e6e3a: Pull complete 
088e651df7e9: Pull complete 
ed155773e5e0: Pull complete 
ffebb35d2904: Pull complete 
293f0bec643a: Pull complete 
1655a257a5b5: Pull complete 
4ddba458499d: Pull complete 
90e48ae03559: Pull complete 
822c1a513e6a: Pull complete 
Digest: sha256:1bf73ccae25238fa555100080042f0b2f9be08eb757e200fe6afc1fc413a1b3c
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

三、部署Postgresql数据库

3.1运行docker 容器

[root@centos ~]#mkdir /data/app/postgres
[root@centos ~]# docker run -d --name postgres --restart always -e POSTGRES_PASSWORD='admin'   -e POSTGRES_USER='admin' -e ALLOW_IP_RANGE=0.0.0.0/0 -v /data/app/postgres/data:/var/lib/postgresql  -v /etc/localtime:/etc/localtime -p 55433:5432 -d postgres
ce17290709e537a7a9cc4e1ceb477210f14d5eded4adda3484e76d2cd48e399b

四、检查容器情况

查看容器运行状态

[root@centos ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS              PORTS                                                  NAMES
ce17290709e5   postgres    "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:55433->5432/tcp, :::55433->5432/tcp            postgres

五、安装pgadmin管理数据库

[root@centos ~]# docker run -d -p 5433:80 --name pgadmin4 -v /etc/localtime:/etc/localtime -e PGADMIN_DEFAULT_EMAIL=test@123.com -e PGADMIN_DEFAULT_PASSWORD=123456 dpage/pgadmin4
[root@centos ~]# docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED              STATUS              PORTS                                                  NAMES
e8d48f1de9d1   dpage/pgadmin4   "/entrypoint.sh"         About a minute ago   Up About a minute   443/tcp, 0.0.0.0:5433->80/tcp, :::5433->80/tcp         pgadmin4

浏览器访问:http://192.168.60.22:5433

输入账号密码后在“仪表盘”点击“添加服务器”

docker部署Postgresql数据库插图

六、其他说明

5.1修改容器debian的源

debian镜像_debian下载地址_debian安装教程-阿里巴巴开源镜像站 (aliyun.com)

debian12中使用了新配置方式,用下面这一行替换 sed -i ‘s|deb.debian.org|mirrors.aliyun.com|g’ /etc/apt/sources.list.d/debian.sources

###查看容器的ID
[root@centos ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS        PORTS                                                  NAMES
57e1db0764fa   postgres    "docker-entrypoint.s…"   16 hours ago    Up 16 hours   0.0.0.0:55433->5432/tcp, :::55433->5432/tcp      
###进入容器
[root@centos ~]# docker exec -it 57e1db0764fa bash
root@57e1db0764fa:/# sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources
root@57e1db0764fa:/etc/apt# apt update