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.

  • How to display the docker current configuration?
  • How to import my db dump into mariadb on start?
  • DC/OS JMX Access to java application
  • Runc Containerd Check pointing support
  • Disable logging on interactive containers
  • MySQL Connection in Docker container
  • 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 []


    version: '2'
            image: postgres
            build: .
                - .:/code
                - "8000:8000"
            command: bash -c "./ db:5432 && python migrate && python runserver --noreload"
                - db


        "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 = ('', 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.


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

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


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

  • plugin/docker : Is passing id_rsa priv key using build_args correct?
  • Docker swarm mode - no published ports on worker nodes
  • Containers dependency with etcd keys
  • Pecl doesn't work in php:7.0-fpm image
  • Weblogic + Docker + Vagrant = Connection Issue
  • Fedora seems to not support btrfs
  • 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 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 file without all the waiting / breaking into the debugger nonsense, and enjoy a much more robust debugging experience.

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

    run teh server:

    python runserver --noreload

    Hope this helps!

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

    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 = ('', 3000))

    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/"]
    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.