Hot Reloading for FastAPI Docker Debugger #3362
-
Hi there! I really love being able to use the vs code debugger when running my containerized FastAPI app <3! It's super awesome! I've tried both setting --reload in Dockerfile and in tasks.json but it doesn't seem to work :/. Am I doing it wrong or is it still not implemented? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Changing this over to a Discussion. There's one important additional step required aside from adding "dockerRun": {
"volumes": [
{
"containerPath": "/app",
"localPath": "${workspaceFolder}"
}
]
} Here's my full tasks.json{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "fastapi:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"args": [
"main:app",
"--reload", // This is added
"--host",
"0.0.0.0",
"--port",
"8000"
],
"module": "uvicorn"
},
"dockerRun": { // This is added
"volumes": [
{
"containerPath": "/app",
"localPath": "${workspaceFolder}"
}
]
}
}
]
} I did notice that the browser launched to the app page before the app was completely ready, yielding an error page, but manually F5'ing to reload fixed it. |
Beta Was this translation helpful? Give feedback.
Changing this over to a Discussion.
There's one important additional step required aside from adding
--reload
. By default, the Dockerfile will cause the app code to be copied into the container--modifying it on the source will not modify it in the container until the image is rebuilt. You can override this by adding the following to thedocker-run
task in tasks.json, in addition to the--reload
argument:Here's my full
tasks.json
file:tasks.json