运行 docker-compose.yml 时无法创建数据库

Cannot create database when running docker-compose.yml

当运行ning docker-compose up -d时,我希望创建2个数据库。

docker-compose.yml:

version: '3.4'

volumes:
  db_data:

services:
  postgres:
    image: postgres:alpine
    environment:
      - POSTGRES_PASSWORD=Password123
      - POSTGRES_DB=database1
    ports:
      - "5432:5432"

  platform:
    image: image1/platform:${TAG:-latest}
    build:
      context: .
      dockerfile: PlatformApi/Dockerfile
    restart: on-failure
    environment:
      - ASPNETCORE_ENVIRONMENT=Local
      - ConnectionStrings__DefaultConnection=Server=postgres;Port=5432;Uid=postgres;Pwd=Password123;Database=database1
    ports:
      - "5001:80"
    depends_on:
      - postgres
    volumes:
      - .docker/setup.sql:/docker-entrypoint-initdb.d/setup.sql
      - db_data:/var/lib/mysql

  identity:
    image: image2/identity:${TAG:-latest}
    build:
      context: .
      dockerfile: Identity/Dockerfile
    restart: on-failure
    environment:
      - ASPNETCORE_ENVIRONMENT=Local
      - ConnectionStrings__DefaultConnection=Server=postgres;Port=5432;Uid=postgres;Pwd=Password123;Database=database2
    ports:
      - "5002:80"
    depends_on:
      - postgres
    volumes:
      - .docker/setup.sql:/docker-entrypoint-initdb.d/setup.sql
      - db_data:/var/lib/mysql

这是我的 setup.sql 文件,位于 .docker 文件夹中

CREATE DATABASE IF NOT EXISTS database1;

CREATE USER postgres IDENTIFIED BY Password123;
GRANT CREATE, ALTER, INDEX, LOCK TABLES, REFERENCES, UPDATE, DELETE, DROP, SELECT, INSERT ON database1.* TO postgres;

CREATE DATABASE IF NOT EXISTS database2;

CREATE USER postgres IDENTIFIED BY Password123;
GRANT CREATE, ALTER, INDEX, LOCK TABLES, REFERENCES, UPDATE, DELETE, DROP, SELECT, INSERT ON database2.* TO postgres;

FLUSH PRIVILEGES;

当我运行docker-compose up -d时,创建了3个容器,但其中1个因错误退出database "database2" does not exist.

我做错了什么? setup.sql文件没有执行或者内容不正确?

在 postgres 服务中,您使用以下配置启动数据库

  postgres:
    image: postgres:alpine
    environment:
      - POSTGRES_PASSWORD=Password123
      - POSTGRES_DB=database1
    ports:
      - "5432:5432"

因此创建了一个名为 database1 的数据库

在以下服务中您尝试先连接数据库


在平台服务中:

  - ConnectionStrings__DefaultConnection=...;Database=database1

这里没有问题,因为 database1 存在


但是在identity服务中:

- ConnectionStrings__DefaultConnection=...;Database=database2

您尝试连接到 database2 does not exist


不存在的原因是你的setup.sql里有race conditions。您不能保证 identity 服务启动时,database2 已经由 platform 服务创建。


要解决这个问题,您可以添加 postgres2 服务来创建 database2

  postgres2:
    image: postgres:alpine
    environment:
      - POSTGRES_PASSWORD=Password123
      - POSTGRES_DB=database2
    ports:
      - "5433:5432"

  identity:
    image: image2/identity:${TAG:-latest}
    build:
      context: .
      dockerfile: Identity/Dockerfile
    restart: on-failure
    environment:
      - ASPNETCORE_ENVIRONMENT=Local
      - ConnectionStrings__DefaultConnection=Server=postgres2;Port=5432;Uid=postgres;Pwd=Password123;Database=database2
    ports:
      - "5002:80"
    depends_on:
      - postgres2
    volumes:
      - .docker/setup.sql:/docker-entrypoint-initdb.d/setup.sql
      - db_data:/var/lib/mysql