Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to run backend separately #257

Closed
orihomie opened this issue Sep 8, 2020 · 12 comments
Closed

Ability to run backend separately #257

orihomie opened this issue Sep 8, 2020 · 12 comments

Comments

@orihomie
Copy link

orihomie commented Sep 8, 2020

For now its up to 2 days while I'm struggling with postgres docker container and it would've been great if there was any guidelines/ ready compose file to launch them locally and debug that locally.

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

@tiangolo I appreciate what you did for us all but please, mention somewhere that default values you've put int /backend/app/alembic/env.py

def get_url():
    user = os.getenv("POSTGRES_USER", "postgres")
    password = os.getenv("POSTGRES_PASSWORD", "")
    server = os.getenv("POSTGRES_SERVER", "db")
    db = os.getenv("POSTGRES_DB", "app")
    return f"postgresql://{user}:{password}@{server}/{db}"

@wedwardbeck
Copy link
Sponsor

Do you mean run the DB locally and FastAPI backend locally in a terminal? for env.py include your .env file and then use python-dotenv:
`from future import with_statement

import os

from alembic import context
from dotenv import load_dotenv
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
load_dotenv()
`
Same concept for the backend API which his referenced here.

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

No, I meant to startup backend project locally via docker compose

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

that's my yml file

# Use postgres/example user/password credentials
version: '3.1'
services:

  backend:
    image: backend
    ports:
    - 10088:10088

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_SERVER: server
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pwd
      POSTGRES_DB: db
    ports:
      - 5432:5432

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

and that what I'm passing to settings

kwargs = {
    'SERVER_NAME': '',
    'SERVER_HOST': 'http://127.0.0.1:10088',

    'PROJECT_NAME': 'CookieBack',

    'SENTRY_DSN': '',
...
    'POSTGRES_SERVER': '127.0.0.1:5432',
    'POSTGRES_USER': 'user',
    'POSTGRES_PASSWORD': 'pwd',
    'POSTGRES_DB': 'db',
    'SQLALCHEMY_DATABASE_URI': 'postgresql://user:pwd@127.0.0.1:5432/db',

    'FIRST_SUPERUSER': 'example@mymail.ru',
    'FIRST_SUPERUSER_PASSWORD': 'example'}

settings = Settings(**kwargs)

by

docker-compose -f stack.yml up --force-recreate --build

Still I'm getting this:

backend_1  | WARNING:__main__:Finished call to '__main__.init' after 17.066(s), this was the 18th time calling it.
backend_1  | INFO:__main__:Starting call to '__main__.init', this is the 19th time calling it.
backend_1  | ERROR:__main__:(psycopg2.OperationalError) could not connect to server: Connection refused
backend_1  |    Is the server running on host "127.0.0.1" and accepting
backend_1  |    TCP/IP connections on port 5432?

How I've could fix that?

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

It seems something wrong with my yml setting, because when I'm trying to access postgre container this what I get:

(pythonProject) ➜  backend docker exec -it 8b3340b4f924 bash
root@8b3340b4f924:/app# curl -v -sL -I localhost:5432/ping
* Expire in 0 ms for 6 (transfer 0x56521c6abf50)
* Expire in 1 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 1 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 1 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 1 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 1 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
* Expire in 0 ms for 1 (transfer 0x56521c6abf50)
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 149999 ms for 3 (transfer 0x56521c6abf50)
* Expire in 200 ms for 4 (transfer 0x56521c6abf50)
* connect to 127.0.0.1 port 5432 failed: Connection refused
*   Trying ::1...
* TCP_NODELAY set
* Expire in 149999 ms for 3 (transfer 0x56521c6abf50)
* Immediate connect fail for ::1: Cannot assign requested address
*   Trying ::1...
* TCP_NODELAY set
* Expire in 149999 ms for 3 (transfer 0x56521c6abf50)
* Immediate connect fail for ::1: Cannot assign requested address
* Failed to connect to localhost port 5432: Connection refused
* Closing connection 0

@wedwardbeck
Copy link
Sponsor

If you are running the DB in docker along with the API as you yaml file indicates, you need to refer to the DB by the docker name, not 127.0.0.1. You're passing it the localhost IP and the message shows nothing it talking on that IP & port. Try changing
'POSTGRES_SERVER': '127.0.0.1:5432', to 'POSTGRES_SERVER=db:5432,

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

If you are running the DB in docker along with the API as you yaml file indicates, you need to refer to the DB by the docker name, not 127.0.0.1. You're passing it the localhost IP and the message shows nothing it talking on that IP & port. Try changing
'POSTGRES_SERVER': '127.0.0.1:5432', to 'POSTGRES_SERVER=db:5432,

No, thats not what helped, but what really helped is to get postgre container IP by running:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' b154e0fe2bda

Taking that IP and then putting it to SQLALCHEMY_DATABASE_URI and to

def get_url():
    user = os.getenv("POSTGRES_USER", "example")
    password = os.getenv("POSTGRES_PASSWORD", "example")
    server = os.getenv("POSTGRES_SERVER", '172.18.0.3:5432')
    db = os.getenv("POSTGRES_DB", "example")
    return f"postgresql://{user}:{password}@{server}/{db}"

UPD
Still quoted answer is working - checked it, thanks.

@wedwardbeck
Copy link
Sponsor

and when your IP changes? This is why you refer to the service name.

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

Yes, I've got this, just needed to launch that at least once

@orihomie
Copy link
Author

orihomie commented Sep 8, 2020

I'll fork and put that mention to readme and then close this issue.

orihomie added a commit to orihomie/full-stack-fastapi-postgresql that referenced this issue Sep 9, 2020
@orihomie
Copy link
Author

orihomie commented Sep 9, 2020

#258

@orihomie orihomie closed this as completed Sep 9, 2020
@tiangolo
Copy link
Owner

tiangolo commented Nov 9, 2022

Thanks for the help here @wedwardbeck! 🍰

Thanks for coming back to close this issue @orihomie

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants