Skip to content

Commit 10521cf

Browse files
authored
Merge pull request #1 from cachuperia/instruction
feat: instructions for `VS Code` and `PyCharm`
2 parents f96cb80 + 3df9e21 commit 10521cf

22 files changed

+270
-34
lines changed

.dockerignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
*
22
# Include useful:
3-
!poetry.lock
43
!pyproject.toml
54
!README.md
65
!mwe_fastapi_debug
7-
!tests

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "VS Code: Attach to Debugger",
9+
"type": "python",
10+
"request": "attach",
11+
"port": 5678,
12+
"host": "localhost",
13+
"pathMappings": [
14+
{
15+
"localRoot": "${workspaceFolder}",
16+
"remoteRoot": "/app"
17+
}
18+
]
19+
}
20+
]
21+
}

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ EOF
1717

1818
FROM python:3.11.3-slim-bullseye as app
1919
ENV PATH="/app/.venv/bin:$PATH"
20+
WORKDIR /app
2021
COPY --from=base /app /app
2122
CMD mwe-fastapi

README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,152 @@
11
# 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+
![VSCode debugger](docs/vscode_breakpoint.png)
64+
65+
```shell
66+
# Local shell
67+
curl 192.168.56.5:8000
68+
```
69+
70+
### Remote SSH
71+
72+
![VSCode debugger](docs/vscode_breakpoint_remote.png)
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+
![PyCharm Debug Server](docs/pycharm_debug_server.png)
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+
![PyCharm Debug Server](docs/pycharm_breakpoint.png)
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+
```

Vagrantfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
55
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
66
sudo apt install containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-compose-plugin -y
77
sudo usermod -aG docker vagrant
8+
git clone https://github.com/cachuperia/mwe-fastapi-debug.git
89
sudo reboot
910
SCRIPT
1011

@@ -13,14 +14,15 @@ Vagrant.configure("2") do |config|
1314

1415
config.vm.define "focal64", primary: true do |c|
1516
c.vm.box = "ubuntu/focal64"
16-
config.vm.network "private_network", ip: "192.168.56.4"
17+
config.vm.box_version = "20230619.0.0"
18+
config.vm.network "private_network", ip: "192.168.56.5"
1719
end
1820

1921
config.vm.provider "virtualbox" do |vb|
20-
vb.memory = 4096
21-
vb.cpus = 2
22+
vb.memory = 2048
23+
vb.cpus = 1
2224
end
2325

24-
config.vm.provision "shell", inline: $script
26+
config.vm.provision "shell", privileged: false, inline: $script
2527

2628
end

docker-compose.debug-pycharm.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: "3.8"
2+
3+
services:
4+
app:
5+
extends:
6+
file: docker-compose.debug.yml
7+
service: app
8+
environment:
9+
PYCHARM_DEBUGGER: "1"
10+
expose:
11+
- ${DEBUGGER_PORT:-5678}
12+
network_mode: "host"
13+
14+
volumes:
15+
- ./pydevd-pycharm.egg:/app/pydevd-pycharm.egg

docker-compose.debug-vscode.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "3.8"
2+
3+
services:
4+
app:
5+
extends:
6+
file: docker-compose.yml
7+
service: app
8+
command:
9+
- sh
10+
- -c
11+
- pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:${DEBUGGER_PORT:-5678} -m mwe_fastapi_debug.main
12+
ports:
13+
- ${DEBUGGER_PORT:-5678}:${DEBUGGER_PORT:-5678}

docker-compose.debug.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
app:
3+
extends:
4+
file: docker-compose.yml
5+
service: app
6+
volumes:
7+
- ./mwe_fastapi_debug:/app/mwe_fastapi_debug

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ version: '3.8'
22

33
services:
44
app:
5-
image: local/mwe-fastapi-debug:latest
5+
image: local/mwe-fastapi:latest
66
build:
77
context: .
88
ports:
9-
- "8000:8000"
9+
- ${APP_PORT:-8000}:${APP_PORT:-8000}

0 commit comments

Comments
 (0)