You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One of the missing things is a description of how to debug back-end services in Docker. I couldn't successfully manage how to debug in VSCode (via ptvsd) or PyCharm (via remote debug option). Probably some Traefik config should be changed in order to allow remote connections to interpreter.
I've tried this approach and it's not working for me, at least using the latest ptvsd
i can connect to the debugger but breakpoints aren't hit
any help would be appreciated!
main.py
import ptvsd
from flask import Flask
app = Flask(__name__)
ptvsd.enable_attach(log_dir='/var/log/')
@app.route("/")
def hello():
return "Hello World from Flask!"
if __name__ == "__main__":
# Only for debugging while developing
app.run(host='0.0.0.0', debug=True, port=80)
I cannot help with VSCode, but I'd like to leave an instruction on how to do the same thing when using Pycharm. To debug backend app deployed locally using docker-compose, my preferred way is to use Pycharm's remote debugger.
Few caveats that require a little explanation when debugging project deployed in a local docker environment:
In a 'run/debug configuration' window, as a Local host name use localhost. When creating a mapping, use the same mapping that is defined in docker-compose.dev.volumes.yml, which is <path in your host filesystem>/backend/app=/app.
Installation of the pydevd-pycharm~=<version of PyCharm on the local machine> can be added in backend/backend.dockerfile, just append "pydevd-pycharm~=193.6494" at the end of the pip install run. Version can be checked using Pycharm's menu bar -> help -> about.
Finally, add import pydevd_pycharm pydevd_pycharm.settrace('172.22.0.1', port=12345, stdoutToServer=True, stderrToServer=True) in backend/app/app/main.py. Replace IP address, with an address of your host. It can be checked using docker inspect <container-id-or-name> | grep Gateway (e.g. docker inspect projectslug_proxy_1 | grep Gateway).
It would be even better to use some env var to enable debugging only in development.
Now you can run your debugger, then docker-compose up and voila!
Hello! @tiangolo, i try instruction by @LukGerman, but can't debug backend via pycharm.
I need to put breakpoint to code,
Is it possible?
do you help me?
Thanks a lot!
Add a pycharm configuration to run that file. Select your local main.py file and the remote python interpreter you created in step 2. Make sure the working directory is backend/app
Modify docker-compose-override.yml to expose backend port 8567, by adding the following line - "8567:8567" under - "8888:8888".
Modify your frontend to talk to that port, by adding :8567 to envApiUrl for dev in env.ts. (Skip this step if you don't need the frontend).
Add CORS for that port, by adding it to your .env.
Then to run it:
docker-compose up (as usual)
run or debug your configuration from step 3. It should now kill the backend service created by docker-compose and replace it with its own.
If anyone knows more about traefik and docker, there is probably an easier way that doesn't require me to add a new backend port and hardcode it in the frontend. Please tell me if there is :)
In light of @sepharg's suggestion, using debugpy which is the replacement of ptvsd(for VSCode users, or Vim/Emacs+DAP users), adapted to the current structure of the project:
Add the following to the settings at backend/app/core/config.py:
DEBUG_MODE: bool=FalseWAIT_FOR_ATTACH: bool=False
Add the following dev depenency to backend/app/pyproject.toml, under [tool.poetry.dev-dependencies]: debugpy = "^1.2"
Add the following code to backend/app/main.py, right before the FastAPI constructor:
Optionally(not related to the debugger), add the argument debug=settings.DEBUG_MODE to the FastAPI constructor to have stacktraces upon API endpoint errors.
Add the port mapping
- "5432:5432"
(or any other external port you wish) to docker-compose.override.yml under services.backend.ports
Add DEBUG_MODE=True to the .env file and optionally WAIT_FOR_ATTACH=True
At this point, you should be able to connect to localhost:5432 via any debugger that supports DAP
For VSCode users, make the backend/app/.vscode/launch.json as follows: (assuming you open the VSCode editor at the backend/app folder)
@nmdanny provided excelent instructions, unfortunately a typo on the port number, 5432 is for postgres, they should have been 5678, the default port for debugpy.
Another minor issue is in the pathmappings, if the IDE workspace, is the project root folder, should use:
Thanks @nmdanny and @okz for your solutions. It works for me.
Question for @okz :
Regarding 5432 being the default port for postgres -
Aren't we using port 5050 for postgres for this project? Why is it blocked then?
I'm newbie here. Sorry if the question is too naive
Activity
tiangolo commentedon Apr 15, 2019
Thanks for the report, I'll take a look into this.
plankter commentedon Apr 15, 2019
It seems the solution is quite simple: one can add the following lines in main.py:
and then use such VSCode launch.json config as:
sepharg commentedon May 28, 2019
I've tried this approach and it's not working for me, at least using the latest ptvsd
i can connect to the debugger but breakpoints aren't hit
any help would be appreciated!
main.py
launch.json (i'm on windows)
Dockerfile
requirements.txt
LukGerman commentedon Feb 23, 2020
I cannot help with VSCode, but I'd like to leave an instruction on how to do the same thing when using Pycharm. To debug backend app deployed locally using docker-compose, my preferred way is to use Pycharm's remote debugger.
Here is a documentation on how to set it up.
Few caveats that require a little explanation when debugging project deployed in a local docker environment:
localhost. When creating a mapping, use the same mapping that is defined indocker-compose.dev.volumes.yml, which is<path in your host filesystem>/backend/app=/app.pydevd-pycharm~=<version of PyCharm on the local machine>can be added inbackend/backend.dockerfile, just append"pydevd-pycharm~=193.6494"at the end of the pip install run. Version can be checked using Pycharm's menu bar -> help -> about.import pydevd_pycharm pydevd_pycharm.settrace('172.22.0.1', port=12345, stdoutToServer=True, stderrToServer=True)inbackend/app/app/main.py. Replace IP address, with an address of your host. It can be checked usingdocker inspect <container-id-or-name> | grep Gateway(e.g.docker inspect projectslug_proxy_1 | grep Gateway).It would be even better to use some env var to enable debugging only in development.
Now you can run your debugger, then
docker-compose upand voila!cuzox commentedon Mar 22, 2020
What's up with this @tiangolo ?
This project is great, but not knowing how to debug it makes development a tedious headache.
mashkovd commentedon Jun 8, 2020
Hello!
@tiangolo, i try instruction by @LukGerman, but can't debug backend via pycharm.
I need to put breakpoint to code,
Is it possible?
do you help me?
Thanks a lot!
NMisko commentedon Apr 19, 2021
For pycharm i got it working by doing this:
backendservicerun.pynext tomain.pymain.pyfile and the remote python interpreter you created in step 2. Make sure the working directory isbackend/appdocker-compose-override.ymlto expose backend port8567, by adding the following line- "8567:8567"under- "8888:8888".:8567toenvApiUrlfordevinenv.ts. (Skip this step if you don't need the frontend)..env.Then to run it:
docker-compose up(as usual)If anyone knows more about traefik and docker, there is probably an easier way that doesn't require me to add a new backend port and hardcode it in the frontend. Please tell me if there is :)
nmdanny commentedon Apr 23, 2021
In light of @sepharg's suggestion, using debugpy which is the replacement of ptvsd(for VSCode users, or Vim/Emacs+DAP users), adapted to the current structure of the project:
Add the following to the settings at
backend/app/core/config.py:Add the following dev depenency to
backend/app/pyproject.toml, under[tool.poetry.dev-dependencies]:debugpy = "^1.2"Add the following code to
backend/app/main.py, right before theFastAPIconstructor:Optionally(not related to the debugger), add the argument
debug=settings.DEBUG_MODEto theFastAPIconstructor to have stacktraces upon API endpoint errors.Add the port mapping
- "5432:5432"(or any other external port you wish) to
docker-compose.override.ymlunderservices.backend.portsAdd
DEBUG_MODE=Trueto the.envfile and optionallyWAIT_FOR_ATTACH=TrueAt this point, you should be able to connect to
localhost:5432via any debugger that supports DAPFor VSCode users, make the
backend/app/.vscode/launch.jsonas follows: (assuming you open the VSCode editor at thebackend/appfolder){ "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "localhost", "port": 5432 }, "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/app" } ] } ] }This has worked for me, using Docker on Windows (WSL2 engine) and VSCode
okz commentedon Jul 20, 2022
@nmdanny provided excelent instructions, unfortunately a typo on the port number, 5432 is for postgres, they should have been 5678, the default port for debugpy.
Another minor issue is in the pathmappings, if the IDE workspace, is the project root folder, should use:
mshajarrazip commentedon Jun 12, 2023
@nmdanny's setup works for me - using docker on Ubuntu 20.04
phil037 commentedon Aug 13, 2023
Thanks @nmdanny and @okz for your solutions. It works for me.
Question for @okz :
Regarding 5432 being the default port for postgres -
Aren't we using port 5050 for postgres for this project? Why is it blocked then?
I'm newbie here. Sorry if the question is too naive