7

trying to get remote debugging for my python flask API I have. I'm able to docker-compose up and have postman successfully call the running container, but when I try to attach the debugger, it never compiles. Below is my yml, dockerfile and vscode launch settings... the following error I get is:

There was an error in starting the debug server. Error = {"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5050}

version: '2'

services:
  website:
    build: .
    command: >
      python ./nomz/app.py
    environment:
      PYTHONUNBUFFERED: 'true'
    volumes:
      - '.:/nomz'
    ports:
      - '5000:5000'
      - '5050'

DockerFile

FROM python:3.6-slim

ENV INSTALL_PATH /nomz
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

EXPOSE 5000 5050

VSCode Launch Settings

{
  "name": "Python: Attach",
  "type": "python",
  "request": "attach",
  "localRoot": "${workspaceFolder}/nomz/app.py",
  "remoteRoot": "/nomz/",
  "port": 5050,
  "host": "localhost"
}
2
  • Please give the experimental debugger a try and see if that resolve the issue. Also make sure to have ptvsd installed in the Docker container (code.visualstudio.com/docs/python/debugging#_remote-debugging). Commented Jun 11, 2018 at 19:31
  • Thanks, I just got it working with the reg debugger finally!. It is also very import that the ptvsd is 3.0.0 even though there is a newer release (3.2.1) Commented Jun 12, 2018 at 5:50

1 Answer 1

4

I finally got it working with remote debugging. I had to pip3 install ptvsd==3.0.0 on my local, and make sure that the requirements.txt for my docker container had the same version. (note: the latest version 3.2.1 didn't work)

@BrettCannon had the right link for a good tutorial https://code.visualstudio.com/docs/python/debugging#_remote-debugging

What I had to do was add some code to the app.py of the flask app. I originally was getting address already in use error when starting the container, so I added the socket code and after the first successful attach of debugger I didn't seem to need it anymore (strange I know, but that's why I left it in in case someone else gets that error)

try:
    import ptvsd
    # import socket
    # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # sock.close()
    ptvsd.enable_attach(secret=None,address=('0.0.0.0',5050))
    ptvsd.wait_for_attach()
except Exception as ex:
    print('Not working: ')

also i took the debug kwarg off the app.run() in app.py for the flask app. This all gave me the ability to connect the debugger, but the breakpoints where "Unverified", so the last thing that had to happen was a path to the app.py in the launch.json for the remoteRoot. I will say I created a small test api to get this working, and it only need the first level of pathing (ie. /app and not /app/app/app.py)Here is a github of the test api I made (https://github.com/tomParty/docker_python). So if the debugger is attaching, but your breakpoints are unverified, play around with the path of the remoteRoot

"remoteRoot": "/nomz/nomz/app.py"
Sign up to request clarification or add additional context in comments.

3 Comments

I got the debugger to connect using your github code, but it doesn't stop execution at my breakpoints when I access the site in the browser at localhost:3000. Any help would be appreciated.
Nevermind, I had to put the ptvsd code at the top of the file.
Everything is specified in this blog post vinta.ws/code/… try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.