Home > Blockchain >  Api-platform mongoldb usage without having postgress in docker-compose
Api-platform mongoldb usage without having postgress in docker-compose

Time:11-02

I am able to configure usage of ODM and Mongodb with api-platform using official doc. But you still need to define service for postgress (and have orm dependencies installed), otherwise on startup app waits for db and fails on timeout:

php_1         | Waiting for database to be ready...
php_1         | Still waiting for database to be ready... Or maybe the database is not reachable. 59 attempts left.
php_1         | Still waiting for database to be ready... Or maybe the database is not reachable. 58 attempts left.

Here is databases part of docker-compose:

  database:
    image: postgres:13-alpine
    environment:
      - POSTGRES_DB=api
      - POSTGRES_PASSWORD=!ChangeMe!
      - POSTGRES_USER=api-platform
    volumes:
      - db_data:/var/lib/postgresql/data:rw
      # you may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./api/docker/db/data:/var/lib/postgresql/data:rw
    expose:
      - 5433

  db-mongodb:
    # In production, you may want to use a managed database service
    image: mongo
    environment:
      - MONGO_INITDB_DATABASE=api
      - MONGO_INITDB_ROOT_USERNAME=api-platform
      # You should definitely change the password in production
      - MONGO_INITDB_ROOT_PASSWORD=!ChangeMe!
    volumes:
      - db_data:/var/lib/mongodb/data:rw
      # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/db/data:/var/lib/mongodb/data:rw
    ports:
      - 27017

Any hints what should I change in my configuration to be able to delete database service? I want to keep my setup clean.

CodePudding user response:

According to https://github.com/api-platform/api-platform/blob/main/docker-compose.yml by default php service depends_on: database service. Try removing that line or replacing it with your db-mongodb service and then remove database service definition from your docker-compose.yaml.

depends_on is used to control starting and shutting down containers in the correct order according to defined dependencies, you can find more info here.

CodePudding user response:

The php service is started before the db-mongodb service.

The depends_on options is not fix this that problem.

Check https://docs.docker.com/compose/startup-order/

The simplified version of solution above is to add mongo-db entrypont:

# wait 5 seconds to start mongo in container
while :; do echo '.'; sleep 5 ; done
  • Related