【问题标题】:Postgres Database not being Created by Docker-Compose.yml fileDocker-Compose.yml 文件未创建 Postgres 数据库
【发布时间】:2022-01-24 14:44:49
【问题描述】:

此错误仅发生在我的 4 台设备中的一台上,我正在尝试对其进行调试。此设备是配备 Intel 处理器的 Macbook pro。 数据库容器(db 服务)启动但不创建数据库。

version: "3.7" 
services:   
  db:
    networks:
      new:
          aliases:
              - database
    restart: always
    container_name: db
    image: postgres:latest
    ports:
      - 5433:5432
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=user
      - POSTGRES_DB=core
      # - PGDATA=/tmp
    volumes:
      - ./pgdata:/var/lib/postgresql/data
  migrate:
    image: migrate/migrate
    depends_on:
      - db
    networks:
        - new
    volumes:
        - ./db/migrations:/migrations
    command: ["-path", "/migrations", "-database",  "postgres://user:password@database:5432/core?sslmode=disable", "up"]
    links: 
        - db
  web:
    networks:
      - new
    build: . 
    ports:
      - "8080:8080"
    volumes:
      - .:/server
    links:
      - db
    depends_on:
      - db
      - redis
    environment:
      PORT: 8080
      CONNECTION_STRING_DEV: db://user:password@db:5433/db
      DSN: "db://user:password@db:5433/core"
  redis:
    networks:
      - new
    image: "redis"
    ports:
      - "6379:6379"
networks:
  new:

容器停在2022-01-19 15:37:02.916 UTC [49] LOG: database system is ready to accept connections 并且从未真正执行过“CREATE DATABASE”

由于未创建数据库,我连接的 Go API 无法正常运行。 docker-compose 应该创建数据库“核心”,启动 redis 实例,然后启动 Web 服务。之后,我通常会拉起进行数据库迁移的迁移容器。当docker-compose up web 运行时,我的所有其他设备(macOS、Windows 和 linux)都可以正常运行并启动数据库。

【问题讨论】:

    标签: postgresql docker go docker-compose dockerfile


    【解决方案1】:

    这里是来自 postgresql docker 镜像页面的警告:

    Warning: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup. One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts.
    

    所以您使用的主机的原因之一是 ./pgdata

    您也有非常详细的文档,说明如何在启动时扩展映像或运行某些东西 - 您实际上可以在第一次启动时清理所有内容。

    https://hub.docker.com/_/postgres

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2019-05-15
      • 1970-01-01
      • 2020-07-06
      • 2022-07-20
      相关资源
      最近更新 更多