Home > Enterprise >  Cannot create database when running docker-compose.yml
Cannot create database when running docker-compose.yml

Time:05-22

When running docker-compose up -d, I expect 2 databases to be created.

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

This is my setup.sql file which is located inside a .docker folder

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;

When I run docker-compose up -d, 3 containers are created but 1 of them is exited with an error database "database2" does not exist.

What did I do wrong? Did the setup.sql file not execute or is the content incorrect?

CodePudding user response:

In the postgres service you initiate the Database with the following config

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

As a result a database with name database1 is created

In the following services you try to firstly connect to the database


In platform service:

  - ConnectionStrings__DefaultConnection=...;Database=database1

Here there is no issue since database1 exists


But in identity service:

- ConnectionStrings__DefaultConnection=...;Database=database2

You try to connect to database2 which does not exist


The reason it does not exist is that there are race conditions in your setup.sql. You can not guarantee that when the identity service initiates, that the database2 would be already created by platform service.


To tackle this, you could add postgres2 service which creates the 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
  • Related