I have a monorepo app, that consists of diamonds-domain which is a common library that has a shared code for DB, Pulsar, Redis and repositories and managers to use those, diamonds-service which is a readonly service written in FastAPI and diamonds-worker which is read write worker to handle all the write operations that initiated through the service. They communicate with each other only through Pulsar and share Redis and DB. For DB work I use sqlalchemy + databases library, I also have alembic for automigrations in my domain-service so it could be the only source of those migrations. I was trying to make dockerfiles for each worker and service and docker-compose file to run this app locally. But It wasn't working because of shared Postgres DB container, while service was working fine, worker was failing with error "the connection already acquired". The pulsar communication was working fine. Worker was written with no framework - pure python. TO solve the problem I've added pgbouncer to my docker-compose but it didn't helped, so I was trying change worker by introducing FastAPI there cause I thought that the problem is in eventloop management, so I thought I could fix it by leaving the mamagement of eventloop on the framework. But instead it not only hasn't solve the problem, but now Pulsar also stopped working in my docker, failing with ConnectionError. Beside that, autogenerated migrations generated only inside DOcker and doesn't appear in versions folder where I need them. Could you help me to fix the Worker and all those Docker problems. Here is my Dockerfile for service:
FROM python:3.11-slim-bullseye

RUN apt-get update && \
    apt-get install -y gcc libpq-dev && \
    apt clean && \
    rm -rf /var/cache/apt/*

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONIOENCODING=utf-8

COPY ./diamonds-domain ./diamonds-domain
COPY ./diamonds-service /src

RUN pip install -U pip && \
    pip install poetry

WORKDIR /src

RUN poetry config virtualenvs.create false && \
    poetry install --no-root --no-interaction --no-ansi

ENV DEFAULT_MODULE_NAME=main

ENV MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME}
ENV VARIABLE_NAME=${VARIABLE_NAME:-app}
ENV APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"}

ENV HOST=${HOST:-0.0.0.0}
ENV PORT=${PORT:-8000}

CMD uvicorn ${APP_MODULE} --reload --proxy-headers --host ${HOST} --port ${PORT}

And here is for worker:
FROM python:3.11-slim-bullseye

RUN apt-get update && \
    apt-get install -y gcc libpq-dev && \
    apt clean && \
    rm -rf /var/cache/apt/*

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONIOENCODING=utf-8

COPY ./diamonds-domain ./diamonds-domain
COPY ./diamonds-worker /src

RUN pip install -U pip && \
    pip install poetry

WORKDIR /src

RUN poetry config virtualenvs.create false && \
    poetry install --no-root --no-interaction --no-ansi

ENV DEFAULT_MODULE_NAME=main

ENV MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME}
ENV VARIABLE_NAME=${VARIABLE_NAME:-app}
ENV APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"}

ENV HOST=${HOST:-0.0.0.0}
ENV PORT=${PORT:-8001}

CMD uvicorn ${APP_MODULE} --reload --proxy-headers --host ${HOST} --port ${PORT}

And here is DOcker-compose file:
services:
  diamonds-service:
    env_file:
      - ./diamonds-service/.env
    build:
      context: .
      dockerfile: diamonds-service/Dockerfile
    container_name: diamonds-service
    image: diamonds_service_image
    environment:
      - DATABASE_URL=postgresql://app:app@pgbouncer:5432/app
      - PULSAR_SERVICE_URL=pulsar://pulsar:6650
    ports:
      - "8000:8000"
    depends_on:
      - pgbouncer
      - app_redis
      - pulsar

  diamonds-worker:
    env_file:
      - ./diamonds-worker/.env
    build:
      context: .
      dockerfile: diamonds-worker/Dockerfile
    container_name: diamonds-worker
    image: diamonds_worker_image
    environment:
      - DATABASE_URL=postgresql://app:app@pgbouncer:5432/app
      - PULSAR_SERVICE_URL=pulsar://pulsar:6650
    ports:
      - "8001:8001"
    depends_on:
      - pgbouncer
      - app_redis
      - pulsar

  app_db:
    container_name: app_db
    hostname: app_db
    image: library/postgres:15.3
    environment:
      - POSTGRES_USER=app
      - POSTGRES_PASSWORD=app
      - POSTGRES_DB=app
    volumes:
      - app_pg_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    mem_limit: 1g
    restart: unless-stopped
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U app" ]
      interval: 30s
      timeout: 10s
      retries: 5

  pgbouncer:
    image: rmccaffrey/pgbouncer:latest
    container_name: pgbouncer
    environment:
      - DATABASE_URL=postgresql://app:app@app_db:5432/app
      - POOL_MODE=transaction
      - MAX_DB_CONNECTIONS=100
      - DEFAULT_POOL_SIZE=40
      - AUTH_TYPE=scram-sha-256
      - POOL_MODE=transaction
      - MAX_CLIENT_CONN=100
      - DEFAULT_POOL_SIZE=20
      - RESERVE_POOL_SIZE=5
    depends_on:
      - app_db

  app_redis:
    image: redis:7.0-alpine
    hostname: redis
    container_name: app_redis
    command:
      - 'redis-server'
      - '--requirepass myStrongPassword'
    ports:
      - "6379:6379"
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 30s
      timeout: 5s
      retries: 5

  pulsar:
    container_name: pulsar
    environment:
      - PULSAR_STANDALONE_USE_ZOOKEEPER=1
    image: apachepulsar/pulsar:3.3.2
    volumes:
      - pulsar_data:/pulsar/data
    command: >
      /pulsar/bin/pulsar standalone
    ports:
      - "6650:6650"
      - "8080:8080"
    mem_limit: 2g
    restart: unless-stopped

volumes:
  app_pg_data:
    driver: "local"
  pulsar_data:

networks:
  default:
    external: true
    name: diamonds_app_main