|
1 | 1 | # mwe-fastapi-debug |
| 2 | + |
| 3 | +IDE configuration for debugging an application running inside a remote Docker Compose stack. |
| 4 | + |
| 5 | +In this example, we will be working with an Ubuntu 20.04 virtual machine, but any instance with SSH access can be used. |
| 6 | + |
| 7 | + |
| 8 | +# Bootstrap |
| 9 | + |
| 10 | +This step can be skipped for VS Code [Remote - SSH](https://code.visualstudio.com/docs/remote/ssh) sessions. |
| 11 | + |
| 12 | +```shell |
| 13 | +# Local shell |
| 14 | +git clone https://github.com/cachuperia/mwe-fastapi-debug.git |
| 15 | +cd mwe-fastapi-debug |
| 16 | +``` |
| 17 | + |
| 18 | +SSH credentials and debugger port for forwarding. |
| 19 | + |
| 20 | +Please change these values to your own if you need to connect to a different instance. |
| 21 | + |
| 22 | +```shell |
| 23 | +# Local shell |
| 24 | +DEBUGGER_PORT=5678 |
| 25 | +USER=vagrant |
| 26 | +HOST=192.168.56.5 |
| 27 | +KEY_PATH=.vagrant/machines/focal64/virtualbox/private_key |
| 28 | +SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -S /tmp/%r@%h-%p" |
| 29 | +``` |
| 30 | + |
| 31 | +# Start VM |
| 32 | + |
| 33 | +```shell |
| 34 | +# Local shell |
| 35 | +vagrant up |
| 36 | +``` |
| 37 | + |
| 38 | +# Configure Debugger |
| 39 | + |
| 40 | +## Configure VS Code |
| 41 | + |
| 42 | +You can connect to the instance with debugger server port forwarded |
| 43 | + |
| 44 | +```shell |
| 45 | +# Local shell |
| 46 | +ssh -L ${DEBUGGER_PORT}:localhost:${DEBUGGER_PORT} ${USER}@${HOST} -i ${KEY_PATH} ${SSH_OPTIONS} |
| 47 | +``` |
| 48 | + |
| 49 | +or [Remote SSH](https://code.visualstudio.com/docs/remote/ssh) session. |
| 50 | + |
| 51 | +Start debugging compose stack: |
| 52 | + |
| 53 | +```shell |
| 54 | +# Remote shell |
| 55 | +cd mwe-fastapi-debug |
| 56 | +docker compose -f docker-compose.debug-vscode.yml up |
| 57 | +``` |
| 58 | + |
| 59 | +Start `VS Code: Attach to Debugger` [debug](https://code.visualstudio.com/docs/python/debugging) [configuration](.vscode/launch.json), set breakpoint and send API request. |
| 60 | + |
| 61 | +### Local shell(debugger port should be forwarded from remote) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +```shell |
| 66 | +# Local shell |
| 67 | +curl 192.168.56.5:8000 |
| 68 | +``` |
| 69 | + |
| 70 | +### Remote SSH |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +```shell |
| 75 | +# Remote shell |
| 76 | +curl localhost:8000 |
| 77 | +``` |
| 78 | + |
| 79 | +## Configure PyCharm |
| 80 | + |
| 81 | +Create configuration for `Python Debug Server`, host - `0.0.0.0`, port - `5678`, mapping - `/path/to/project:/app` |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +Start `Python Debug Server`. |
| 86 | + |
| 87 | + |
| 88 | +Connect to the instance with debugger server port forwarded to remote: |
| 89 | + |
| 90 | +```shell |
| 91 | +# Local shell |
| 92 | +ssh -R 0.0.0.0:${DEBUGGER_PORT}:localhost:${DEBUGGER_PORT} ${USER}@${HOST} -i ${KEY_PATH} ${SSH_OPTIONS} |
| 93 | +``` |
| 94 | + |
| 95 | +Copy `pydevd-pycharm.egg` to the remote: |
| 96 | + |
| 97 | +```shell |
| 98 | +# Local shell |
| 99 | +# Change path to debugger egg to yours |
| 100 | +PYCHARM_DEBUGGER=/path/to/PyCharm/debug-eggs/pydevd-pycharm.egg |
| 101 | +scp ${PYCHARM_DEBUGGER} ${USER}@${HOST}:/home/${USER}/mwe-fastapi-debug/ |
| 102 | +``` |
| 103 | + |
| 104 | +Start debugging compose stack: |
| 105 | + |
| 106 | +```shell |
| 107 | +# Remote shell |
| 108 | +cd mwe-fastapi-debug |
| 109 | +docker compose -f docker-compose.debug-pycharm.yml up |
| 110 | +``` |
| 111 | + |
| 112 | +Set Breakpoint and send API request: |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +```shell |
| 117 | +# Local shell |
| 118 | +curl 192.168.56.5:8000 |
| 119 | +``` |
| 120 | + |
| 121 | +## Code modification with "hot (no build) reload" |
| 122 | + |
| 123 | +Modify the service code locally or remotely. |
| 124 | + |
| 125 | +**Make sure that your local code is synchronized with the remote folder [mounted](./docker-compose.debug.yml#L7) as the `/app` folder if you are working locally.** |
| 126 | + |
| 127 | +Next, restart the `docker-compose.debug-<IDE name>.yml` stack. |
| 128 | + |
| 129 | +**If you are working in a Remote SSH VS Code session, ensure that your remote changes are pushed to `origin`.** |
| 130 | + |
| 131 | +# Cleanup |
| 132 | + |
| 133 | +## Close SSH connection |
| 134 | + |
| 135 | +```shell |
| 136 | +# Local shell |
| 137 | +ssh -S /tmp/%r@%h-%p -O exit ${USER}@${HOST} |
| 138 | +``` |
| 139 | + |
| 140 | +## Stop VM |
| 141 | + |
| 142 | +```shell |
| 143 | +# Local shell |
| 144 | +vagrant halt |
| 145 | +``` |
| 146 | + |
| 147 | +## Destroy VM |
| 148 | + |
| 149 | +```shell |
| 150 | +# Local shell |
| 151 | +vagrant destroy -f |
| 152 | +``` |
0 commit comments