Recently i have started using docker for my project running with the following stack
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 files.
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:
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
import debugpy
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()
debugpy.breakpoint()
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
ports:
- "5678:5678"
And my launch.json looks like this
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}/myapp",
"remoteRoot": "."
}
]
}
]
}
NOTE: make sure you have debugpy in your requirements file or install it manually.