I would like to be able to make my code stop on breakpoints with VScode. My project is built with docker-compose and works without debugging on port 8000.
Here are my configurations files:
docker-compose:
version: '3.4'
services:
murmurside:
image: murmurside
build: ./murmur_side
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --host 0.0.0.0 --port 8000"]
volumes:
- ./murmur_side/:/murmur_side/
ports:
- 8000:8000
- 5678:5678
environment:
- DATABASE_URL=postgresql://USERNAME:PASSWORD@db/fastapi_db_2
db:
image: postgres:13-alpine
volumes:
- postgres_data2:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=USERNAME
- POSTGRES_PASSWORD=PASSWORD
- POSTGRES_DB=fastapi_db_2
volumes:
postgres_data2:
dockerfile :
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10-slim
EXPOSE 8000
WORKDIR /murmur_side
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
COPY . /murmur_side
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /murmur_side
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-k", "uvicorn.workers.UvicornWorker", "app.main:app"]
launch.json :
I tested a ‘launch’ configuration but the debugger then bumps on the database related code. It does not seem to properly link to the database : after DATABASE_URL = os.getenv("DATABASE_URL")
DATABASE_URL stays empty.
{
"configurations": [
{
"name": "Docker: Python - Fastapi",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "fastapi"
}
}
]
}
I also tested an ‘attach’ configuration. In that case, I get a debugger container launched at a random port but I get nothing when I browse to 127.0.0.1:randomPort
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 8000 # I also tried with 5678
},
"preLaunchTask": "docker-run: debug",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/murmur_side"
}
]
}
]
}
On this project I see that they added database credentials in the tasks.json. But I did not find any documentation stating that elsewhere. I tried that but I am unsure for the options other than username, password and dbname since I did not have to mention them in the docker-compose. Maybe I am also mistaking on the ports since there are several ones.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "sd4timapi_pip_debug:latest",
"dockerfile": "${workspaceFolder}/murmur_side/Dockerfile",
"context": "${workspaceFolder}/murmur_side/",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"dockerRun": { # I also tried without this section
"image": "sd4timapi_pip_debug:latest",
"volumes": [
{
"containerPath": "/murmur_side/",
"localPath": "${workspaceFolder}/murmur_side/"
}
],
"ports": [
{
"containerPort": 8000,
"hostPort": 8001, # because it doesn't allow me to put 8000 : "port is already allocated"
"protocol": "tcp"
}
],
"env": {
"APP_PORT": "8000", #UNSURE
"DEBUG": "TRUE",
"ENVIRONMENT": "local", #UNSURE
"POSTGRES_USER": "USERNAME",
"POSTGRES_PASS": "PASSWORD",
"POSTGRES_DBNAME": "fastapi_db_2",
"POSTGRES_HOST": "db_host", #UNSURE
"POSTGRES_PORT": "5432", #UNSURE
"POSTGRES_APPLICATION_NAME": "sd4timapi_pip_debug", #UNSURE
}
},
"python": {
"args": [
"app.main:app",
"--host",
"0.0.0.0",
"--port",
"8000"
],
"module": "uvicorn"
}
}
]
}
2
Answers
This works for me, for debugging my FastAPI app in VS Code.
Here’s the ".vscode/launch.json" file:
I get the following messages in the VS Code terminal logs, and then I can debug with breakpoints, etc.
Go to http://localhost:81/docs in your browser since I deployed to port 81.
Same issue, I was able to solve.
docker-compose.debug.yaml:
launch.json:
}
In main.py the FastApi app is named as api:
Hope it helps.