目 录CONTENT

文章目录

用docker-compose管理postgres

Administrator
2025-11-24 / 0 评论 / 0 点赞 / 1 阅读 / 0 字

🤔 用docker-compose管理postgres

Docker 提供了 docker-compose 工具,它使得开发者能够通过一个简单的 YAML 文件来定义和管理多个容器化应用。docker-compose 不仅可以简化单个服务的配置,还能轻松处理多个相互依赖的服务,如数据库、缓存服务器和应用程序服务器等.Docker技术在PostgreSQL数据库部署中的应用与探究

在开发环境使用 Docker Compose 部署 PostgreSQL,让开发环境搭建从"几小时的折腾"变成"一分钟启动",保持不同开发环境的数据隔离和开发协作一致性。使用docker-compose 部署 PostgreSQL + 应用 的架构,核心优势在于简化环境一致性、降低部署复杂度、增强可扩展性。PostgreSQL: The world's most advanced open source database
本文将演示如何在开发环境,容器化部署Postgres:

应用开发和postgres使用示意图

执行 docker-compose up -d 一键启动所有服务,docker-compose down 一键停止并清理,极大简化开发、测试环境的搭建流程。以下是后端应用和postgres连接的拓扑关系示意图:使用端口5432即可连接到postgres.

graph TB
    subgraph "主机环境 Host Machine"
        subgraph "Docker 容器 Docker Container"
            PG[("PostgreSQL 12<br/>服务名: db<br/>主机名: pg.local")]
            PG_DATA[("数据卷<br/>/var/lib/postgresql/data")]
        end
        
        HOST_DATA[("主机存储<br/>./postgres/pgdata_all")]
        HOST_PORT["主机端口<br/>5432"]
    end
    
    subgraph "后端服务 Backend Services"
        APP1["应用服务 1<br/>Backend App"]
        APP2["应用服务 2<br/>Backend App"]
        APP3["应用服务 N<br/>Backend App"]
    end
    
    subgraph "数据库配置 DB Config"
        CONFIG["连接配置<br/>---<br/>Host: localhost/pg.local<br/>Port: 5432<br/>User: service<br/>Password: service<br/>Max Connections: 10000"]
    end
    
    ENV[".env 文件<br/>环境变量"]
    
    %% 数据流
    PG_DATA -.->|数据持久化| HOST_DATA
    HOST_PORT -->|端口映射<br/>5432:5432| PG
    ENV -.->|环境变量注入| PG
    
    %% 连接关系
    APP1 -->|"TCP/IP 连接<br/>postgres://service:service@localhost:5432/dbname"| HOST_PORT
    APP2 -->|"TCP/IP 连接<br/>postgres://service:service@localhost:5432/dbname"| HOST_PORT
    APP3 -->|"TCP/IP 连接<br/>postgres://service:service@localhost:5432/dbname"| HOST_PORT
    
    CONFIG -.->|配置参考| APP1
    CONFIG -.->|配置参考| APP2
    CONFIG -.->|配置参考| APP3
    
    style PG fill:#336791,color:#fff
    style HOST_DATA fill:#e8f4f8
    style PG_DATA fill:#e8f4f8
    style CONFIG fill:#fff4e6
    style ENV fill:#f0f0f0

部署postgres的yaml参数和配置说明

部署postgres配置的docker-compose.yaml

---
# This docker compose file is used for developer to setup
# dev enironment quickly. Only local machine with 
# docker and docker-compose is required.
version: '3.3'
services:
  db:
    image: postgres:12
    restart: always
    volumes:
      # notice: all the db data are located in /root/pgdata
      - ./postgres/pgdata_all:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=service
      - POSTGRES_PASSWORD=service
    command: ["postgres", "-c", "max_connections=10000"]
    env_file:
      - .env
    expose:
      - 5432
    ports:
      - "5432:5432"
    networks:
      - platform-common-net
    hostname: pg.local
networks:
  platform-common-net:
    external: true

参数详细说明

关键参数配置:版本和启动命令

  1. image指定镜像postgres:12✅
  2. 必需restart重启策略always⭐
  3. 推荐volumes数据持久化./data:/var/lib/postgresql/data⭐
  4. 推荐environment环境变量POSTGRES_USER=service⭐
  5. 推荐command自定义命令["postgres", "-c", "max_connections=10000"]
  6. 可选env_file环境变量文件.env

数据持久化: 宿主机和容器映射

- ./postgres/pgdata_all:/var/lib/postgresql/data

作用:数据持久化挂载

  • 左侧:主机目录 ./postgres/pgdata_all(相对于 docker-compose.yml)
  • 右侧:容器内 PostgreSQL 数据目录
  • 效果:容器删除后数据不丢失
主机                        容器
./postgres/pgdata_all  ←→  /var/lib/postgresql/data
    ├── base/                  ├── base/        (数据库文件)
    ├── global/                ├── global/      (全局数据)
    ├── pg_wal/                ├── pg_wal/      (事务日志)
    └── postgresql.conf        └── postgresql.conf

环境变量配置:env_file: - .env

从外部文件加载环境变量,读取 .env 文件中的变量

# 数据库配置
POSTGRES_DB=myapp_dev
POSTGRES_INITDB_ARGS=--encoding=UTF8 --locale=en_US.utf8

# 时区设置
TZ=Asia/Shanghai

# 其他配置
PGDATA=/var/lib/postgresql/data

配置信息示意图


graph TB
    subgraph CONFIG["📄 docker-compose.yml"]
        VERSION["version: '3.3'<br/>指定配置版本"]
        SERVICES["services:<br/>定义服务列表"]
    end
    
    subgraph DB_SERVICE["🐘 db 服务配置"]
        IMAGE["image: postgres:12<br/>使用 PostgreSQL 12 镜像"]
        RESTART["restart: always<br/>自动重启策略"]
        VOLUMES["volumes:<br/>./postgres/pgdata_all<br/>↕️<br/>/var/lib/postgresql/data"]
        ENV["environment:<br/>• POSTGRES_USER=service<br/>• POSTGRES_PASSWORD=service"]
        COMMAND["command:<br/>max_connections=10000"]
        ENV_FILE[".env 文件<br/>额外环境变量"]
        EXPOSE["expose: 5432<br/>内部端口声明"]
        PORTS["ports: 5432:5432<br/>主机端口映射"]
        HOSTNAME["hostname: pg.local<br/>容器主机名"]
    end
    
    subgraph RUNTIME["🚀 运行时效果"]
        CONTAINER["Docker 容器<br/>━━━━━━━━━━<br/>名称: db<br/>主机名: pg.local"]
        DATA_PERSIST["💾 数据持久化<br/>容器删除数据不丢失"]
        NETWORK_ACCESS["🌐 网络访问<br/>• 内部: db:5432<br/>• 外部: localhost:5432"]
        AUTO_RESTART["🔄 自动恢复<br/>崩溃后自动重启"]
        HIGH_CONN["⚡ 高并发支持<br/>最多 10000 连接"]
    end
    
    VERSION --> SERVICES
    SERVICES --> DB_SERVICE
    
    IMAGE --> CONTAINER
    RESTART --> AUTO_RESTART
    VOLUMES --> DATA_PERSIST
    ENV --> CONTAINER
    COMMAND --> HIGH_CONN
    ENV_FILE --> CONTAINER
    EXPOSE --> NETWORK_ACCESS
    PORTS --> NETWORK_ACCESS
    HOSTNAME --> CONTAINER
    
    style CONFIG fill:#f0f4f8,stroke:#495057,stroke-width:2px
    style DB_SERVICE fill:#e7f5ff,stroke:#1971c2,stroke-width:2px
    style RUNTIME fill:#d3f9d8,stroke:#2f9e44,stroke-width:2px
    style VERSION fill:#e9ecef,stroke:#868e96
    style SERVICES fill:#e9ecef,stroke:#868e96
    style IMAGE fill:#a5d8ff,stroke:#339af0
    style RESTART fill:#ffc9c9,stroke:#e03131
    style VOLUMES fill:#b2f2bb,stroke:#37b24d
    style ENV fill:#ffd43b,stroke:#fab005
    style COMMAND fill:#ffa8a8,stroke:#f03e3e
    style ENV_FILE fill:#ffd43b,stroke:#fab005
    style EXPOSE fill:#d0bfff,stroke:#7950f2
    style PORTS fill:#d0bfff,stroke:#7950f2
    style HOSTNAME fill:#c5f6fa,stroke:#15aabf
    style CONTAINER fill:#74c0fc,stroke:#1864ab
    style DATA_PERSIST fill:#8ce99a,stroke:#2b8a3e
    style NETWORK_ACCESS fill:#b197fc,stroke:#6741d9
    style AUTO_RESTART fill:#ff8787,stroke:#c92a2a
    style HIGH_CONN fill:#ffa94d,stroke:#e8590c
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区