Debug python application running in Docker

I’ve just recently begun trying to wrap my head around Docker and have managed to get a development machine up and running. What i’m now trying to do is to be able to use the debugger in Visual Studio Code in my python application (specifically Django).

I’ve tried following the limited documentation of the python extension for VS Code which explains the parameters for remote debugging.

  • Inside Docker container, cronjobs are not getting executed
  • How to connect to containers on docker Overlay network from an external machine
  • Docker + Virtual Box = VT-x is not available (VERR_VMX_NO_VMX)
  • Not enough ram to run whole docker-compose stack
  • Docker container doesn't reload Angular app
  • Can Terraform set a variable from a remote_exec command?
  • Dockerfile

    FROM python:3.5.2
    RUN apt-get update \
    --no-install-recommends && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /code \
    EXPOSE 8000
    WORKDIR /code
    COPY requirements.txt /code
    RUN /bin/bash --login -c "pip install -r requirements.txt"
    ADD . /code
    CMD []
    

    docker-compose.yml

    version: '2'
    services:
        db:
            image: postgres
        web:
            build: .
            volumes:
                - .:/code
            ports:
                - "8000:8000"
            command: bash -c "./wait-for-it.sh db:5432 && python manage.py migrate && python manage.py runserver 0.0.0.0:8000 --noreload"
            depends_on:
                - db
    

    launch.json

    {
        "name": "Attach (Remote Debug)",
        "type": "python",
        "request": "attach",
        "localRoot": "${workspaceRoot}",
        "remoteRoot": "/code",
        "port": 8000,
        "secret": "debug_secret",
        "host": "localhost"
    }
    

    I’ve also added the line ptvsd.enable_attach("debug_secret", address = ('0.0.0.0', 8000)) to one of the project files

    The Issue

    When ever I start the debugger nothing happens and it looks like VS Code is waiting for a breakpoint to hit. But it never does.

    Any ideas?

    EDIT: Minor update

    I have tried using different ports for the debugger aswell as exposing the new ports in docker-compose.yml without any success. It looks like the attach is successfull because the debugger doesn’t crash but no breakpoint is triggered. I’m really stuck on this one.

    Solution

    See answer from theBarkman.
    I’ll add that I was unable to use a secret to get this working. I did the following:

    manage.py

    import ptvsd
    ptvsd.enable_attach(secret=None, address=('0.0.0.0', '3000'))
    

    launch.json

    {
            "name": "Attach Vagrant",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceRoot}",
            "remoteRoot": "/code",
            "port": 3000,
            "secret": "",
            "host":"localhost"
        }
    

  • Is Docker Toolbox or Docker for Mac beneficial over virtualization solutions?
  • docker-compose won't use my named volumes
  • docker service create image command like `docker run`
  • Dockerized Node js app does not start
  • How to change influxdb storage location
  • How to configure a GlassFish instance running on AWS/ElasticBeanstalk/Docker?
  • 2 Solutions collect form web for “Debug python application running in Docker”

    I’ve had the most success remote debugging dockerized Django projects by throwing the ptvsd code into my manage.py file and turning off Django’s live code reload.

    Since Django essentially spins up 2 servers when you runserver (one for that live code reloading, and the other for the actual app server`, ptvsd seems to get really confused which server it should watch. I could sort of get it to work by waiting for attachment, try/excepting the enable_attach method or breaking into the debugger – but breakpoints would never work, and I could only seem to debug a single file at a time.

    If you use the django flag --noreload when spinning up the server, you can throw the ptvsd inside the manage.py file without all the waiting / breaking into the debugger nonsense, and enjoy a much more robust debugging experience.

    manage.py:

    import ptvsd
    ptvsd.enable_attach(secret='mah_secret', address=('0.0.0.0', 3000))
    

    run teh server:

    python manage.py runserver 0.0.0.0:8000 --noreload
    

    Hope this helps!

    I was trying to do something very similar to you and came across this issue/comment:

    https://github.com/DonJayamanne/pythonVSCode/issues/252#issuecomment-245566383

    In there it describes that in order to use breakpoints you need to use the ptvsd.break_into_debugger() function.

    As an example:

    import ptvsd
    ptvsd.enable_attach(secret='my_secret',address = ('0.0.0.0', 3000))
    ptvsd.wait_for_attach()
    ptvsd.break_into_debugger()
    

    As soon as I added this in my python script, my breakpoints worked. Hopefully it’s of some use.


    Edit Jan 24, 2017

    In my DockerFile I installed ptvsd:

    FROM kaixhin/theano
    RUN pip install ptvsd
    WORKDIR /src
    EXPOSE 3000
    ENTRYPOINT ["python","src/app.py"]
    COPY . /src
    

    It looks like your installing dependencies via your requirements.txt file, is ptvsd in your requirements.txt?

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.