Recently i have started using docker for my project running with the following
- Django
- Nginx
- Gunicorn
- Celery
- Postgres
- Redis
Earlier it was easy to setup debugger but after using docker-compose i am not able to do that. As soon as i start the debugger it loads for sometime and then automatically stops with no logs or error anywhere.
Here are the relevant code.
launch.json
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 443
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
}
]
}
docker-compose.yml
version: '3'
services:
db:
image: postgres:12
env_file: .env
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- "5431:5432"
volumes:
- dbdata:/var/lib/postgresql/data
nginx:
image: nginx:1.14
ports:
- "443:443"
- "80:80"
volumes:
- ./config/nginx/:/etc/nginx/conf.d
- ./myapp/static:/var/www/myapp.me/static/
web:
restart: always
build: ./myapp
command: bash -c "
python manage.py collectstatic --noinput
&& python manage.py makemigrations
&& python manage.py migrate
&& gunicorn --certfile=/etc/certs/localhost.crt --keyfile=/etc/certs/localhost.key myapp.wsgi:application --bind 0.0.0.0:443 --reload"
expose:
- "443"
depends_on:
- db
- nginx
env_file:
- .env
volumes:
- ./myapp:/opt/myapp
- ./config/nginx/certs/:/etc/certs
- ./myapp/static:/var/www/myapp.me/static/
broker:
image: redis:alpine
expose:
- "6379"
celery:
build: ./myapp
command: celery -A myapp worker -l info
env_file:
- .env
volumes:
- ./myapp:/opt/myapp
depends_on:
- broker
- db
celery-beat:
build: ./myapp
command: celery -A myapp beat -l info
env_file:
- .env
volumes:
- ./myapp:/opt/myapp
depends_on:
- broker
- db
volumes:
dbdata:
2
Answers
Finally i managed to solve it myself. Few takeaways from the problem.
You need to use debugpy and place that in your manage.py file to start listening to a port. I did something like this
Along with this we need to map this port to a port inside the host machine. For that we need to change and add a single line in web service of docker-compose
And my launch.json looks like this
NOTE: make sure you have debugpy in your requirements file or install it manually.
If you make your debugger a module you can turn it on and off with an environment variable. I just used this article as the basis for a fastapi/gunicorn debugging solution.
See this article by Adrian: Flask Debugging in VS Code