Open JohnnyFee opened 4 years ago
I checked #7 , but no satisfied answer. Can anyone provide best practice for debugging?
How would you normally debug something, what does that look like?
If you create tests then run the debugger through them I'd suggest setting up pytest integration and using pycharm's professional version connect it to the docker compose interpreter. A Google search for the keywords should get you there.
I run the backend individually. Steps as following.
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=settings.SERVER_PORT,
log_level="debug",
reload=True,
workers=1
)
SERVER_NAME: str = ""
SERVER_HOST: AnyHttpUrl = "http://127.0.0.1:10088"
PROJECT_NAME: str = "proName"
SENTRY_DSN: Optional[HttpUrl] = "" # None --> ""
POSTGRES_SERVER: str = "postgres ip" POSTGRES_USER: str = "database user" POSTGRES_PASSWORD: str = "user password" POSTGRES_DB: str = "db name"
SMTP_PORT: Optional[int] = smtp port with ssl (maybe 465) SMTP_HOST: Optional[str] = "smtp server or others" SMTP_USER: Optional[str] = "your email" SMTP_PASSWORD: Optional[str] = "your password" EMAILS_FROM_EMAIL: Optional[EmailStr] = "your email" EMAILS_FROM_NAME: Optional[str] = "your name"
FIRST_SUPERUSER: EmailStr = "your email for admin" FIRST_SUPERUSER_PASSWORD: str = "your password"
4. run main.py --- the project maybe works.
I was able to get it working based on this repo: https://github.com/Kludex/fastapi-docker-debug
I can restart the backend container in debug mode and connect to the session via VS Code.
Launch.json
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}/app",
"remoteRoot": "/app"
}
]
}
]
}
docker-compose.debug.yml
version: "3.3"
services:
backend:
environment:
- SERVER_NAME=${DOMAIN?Variable not set}
- SERVER_HOST=https://${DOMAIN?Variable not set}
# Allow explicit env var override for tests
- SMTP_HOST=${SMTP_HOST}
env_file:
- .env
build:
context: ./backend
dockerfile: backend.dockerfile
args:
INSTALL_DEV: ${INSTALL_DEV-false}
command:
[
"sh",
"-c",
"pip install debugpy && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000",
]
volumes:
- ./backend/app:/app
ports:
- 8000:8000
- 5678:5678
networks:
traefik-public:
# For local dev, don't expect an external Traefik network
external: false
Command to run backend in debug mode
docker-compose -f docker-compose.debug.yml up
I run the project with docker successfully, but I want to know how can I debug backend in PyCharm or VSCode step by step?