skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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.

    # debugger.py
    from os import getenv
    
    def initialize_flask_server_debugger_if_needed():
        if getenv("DEBUGGER") == "True":
            import multiprocessing
    
            if multiprocessing.current_process().pid > 1:
                import debugpy
    
                debugpy.listen(("0.0.0.0", 10001))
                print("⏳ VS Code debugger can now be attached, press F5 in VS Code ⏳", flush=True)
                debugpy.wait_for_client()
                print("� VS Code debugger attached, enjoy debugging �", flush=True)
    

    See this article by Adrian: Flask Debugging in VS Code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search