I try to debug program which is launched on Docker container using VS Code and ptvsd.
Debugger configuration:
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"port": 9091,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src"
}
]
File which should be debugged:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 9091))
ptvsd.wait_for_attach()
while True:
print('elo') # breakpoint is set here
I run container with open port 9091 and code inside container. Then start debugger.
Debugger is attached but it doesn't stop on breakpoint and runs infinite loop.
ptvsd is installed on local and remote with the same version - 4.2.7
What should be changed to make debugger stop on breakpoint?
I also faced the same issue, but here the work around.
Try the following:-
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 9091))
ptvsd.wait_for_attach()
while True:
breakpoint() #this command will add the breakpoint which will detected by debugger
print('elo')
Related
I am curious to know if anyone successfully attached a debugger to a python script -
which is actually an Azure Function App running in a container.
I have all the VSCODE extensions but still can't seem to connect to the running container.
I've run the container from docker-compose.debug and from Bash cmd:
$ docker run -p 5678:5678 -it -e AzureWebJobsStorage="UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://host.docker.internal" nfunc:latest
NB I have to set an environment variable AzureWebJobsStorage for the storage.
My launch.json as follows:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
What am I missing please ..
Can Functions Apps be debugged at all in a container?
Why does it not hit a breakpoint ?
Thank you
In this case, I believe you'll have to do the profiling. You will have to follow these:
If its not a blessed image, then first you would have to install SSH
if you want to get into the container.
Then you will have to make use of tools such as cProfile or other related python modules to profile the code.
Here is a documentation for windows application. You might want to take a look :
https://azureossd.github.io/2017/09/01/profile-python-applications-in-azure-app-services/index.html
This issue has been tracked : https://github.com/Azure/azure-functions-docker/issues/17
I am developing a Python Django app in a Dockerized container. I have successfully setup remote debugging to attach to my Django server inside of a container. Me configuration is as follows.
launch.json
{
"name": "Remote Django App",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"port": 9001,
"host": "localhost"
}
manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'llf_api.settings')
try:
from django.core.management import execute_from_command_line
from django.conf import settings
if settings.DEBUG:
if os.environ.get('RUN_MAIN') or os.environ.get('WERKZEUG_RUN_MAIN'):
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 8001))
print("Attached remote debugger")
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
docker-compose.yml
services:
api:
image: ${DOCKER_IMAGE_BASE}:${DOCKER_TAG}
build:
context: .
dockerfile: ./Dockerfile.development
env_file:
- .env
environment:
- DATABASE_URL=postgres://username:password#db/db_name
volumes:
- .:/app
command: >
bash -c "wait-for-it --service db:5432
&& python3 manage.py runserver 0.0.0.0:8000"
ports:
- "9000:8000"
- "9001:8001"
depends_on:
- db
tty: true
stdin_open: true
The problem is that I run VS Code inside of a dev container (the Dockerfile.development above). So VS Code is essentially running within the same container the Django server is running which would make me think I need to attach to the local port (8001) the ptvsd is running on by setting my launch.json to the following:
launch.json
{
"name": "Local Django App",
"type": "python",
"request": "attach",
"host": "api",
"port": 8001
}
However this doesn't work. When I try to attach the debugger in VS Code it appears to eventually timeout. Does anyone know how this could be accomplished?
My understanding of how VS Code and my server were running was inherently wrong. The server and VS Code are running off of the same image but NOT the same containers. The containers are running side by side and therefore the local networking is not available to either.
To make this work I realized I needed the VS Code container to access the server's container via the debugging port opened on the host. The only way I know how to do that is by using docker.for.mac.localhost as the host. So all that needed to change from my original setup was the launch.json configuration.
launch.json
{
"name": "Remote Django App",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"port": 9001,
"host": "docker.for.mac.localhost"
}
VS Code now attaches to port 9001 which has been exposed on the host and connects to the host using docker.for.mac.localhost. It works!
Create /home/bob/foobar.py in Visual Studio
Code with the VS Code Docker
Extension
import ptvsd
import time
ptvsd.enable_attach(address = ('0.0.0.0', 5678))
ptvsd.wait_for_attach()
time.sleep(2)
print("all righty then")
Set a breakpoint on the last line.
Debug|Add Configuration
In launch.json add to "configurations"
{
"name": "Python Attach (Remote Debug ptsvd default)",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "/home/bob", // You may also manually specify the directory containing your source code.
"remoteRoot": "/home/bob" // Linux example; adjust as necessary for your OS and situation.
}
],
"port": 5678, // Set to the remote port.
"host": "0.0.0.0" // Set to your remote host's public IP address.
},
$ python -m pip install --user --upgrade ptvsd
$ python foobar.py
Start the debugger with configuration "Python Attach (Remote Debug ptsvd default)". It stops at the breakpoint.
But if I run mpirun
$ mpirun --allow-run-as-root -np 2 -H localhost:2 -bind-to none -map-by slot -x PATH -mca pml ob1 -mca btl ^openib python ./foobar.py
I get the error socket.error: [Errno 98] Address already in use
Is there a way to assign multiple ports for an arbitrary number of processes in launch.json and in foobar.py?
This is not currently possible but has been added by the ptvsd team as a possible enhancement. Vote it up if this is important to you. Upvotes for this team seem to move features up the to-do list.
I have lighttpd web server running on my Windows 7 pc using port 80. My web app calls python cgi scripts. I am trying to configure VScode to debug these python scripts when I click a button on one of my html pages. For example:
http://localhost/cgi-bin/instantlight.py?Input=&lightColor=blue
In my launch.json file I have:
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 80,
"secret": "my_secret",
"host": "localhost"
}
When I launch the debugger and click a button, my breakpoint is never hit. After a minute or two I get the error message "Debug adapter process has terminated unexpectedly"
What am I missing?
You don't need to attach to a running process as you're launching the server yourself locally (you only need attach if you're attaching to a server you don't start up yourself). So you can use the e.g. Python: Current File configuration.
I have a virtual machine running a python process and I want to debug it from my host.
My virtual machine ip is 192.168.10.20.
As per the instructions I set up my launch.json as
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "/Users/wrkspace",
"remoteRoot": "/home/wrkspace",
"port": 3000,
"secret": "my_secret",
"host": "192.168.10.20"
},
I have two files,one on my host machine and on my vm called test.py which is a helloworld asking for a user input
My host machine has this at the top of the file
ptvsd.enable_attach("my_secret", address = ('192.168.10.20'3000))
# Enable the line of source code below only if you want the application
to wait until the debugger has attached to it
#ptvsd.wait_for_attach()
While on my vm, where I want to debug
ptvsd.enable_attach("my_secret", address = ('192.168.10.20'3000))
# Enable the line of source code below only if you want the application
to wait until the debugger has attached to it
ptvsd.wait_for_attach()
Once I run my attached debugger and then I kick of the test on my vm by going python test.py. It looks like it does something but then fails with
There was an error in starting the debug server.
Error="code":"ENETUNREACH","errno":"ENETUNREACH","syscall":"connect","address":"192.168.10.20","port":3000}
There was an error in starting the debug server. Error = {"code":"ENETUNREACH","errno":"ENETUNREACH","syscall":"connect","address":"192.168.10.20","port":3000}
Is there something I'm missing?
A few steps to trouble shoot these:
1) test the debug port to make sure it is open using "nc -zv test.com 30302"
2) make sure your webserver is not reloading the app after debugger was connected to it. this is the option for flask: " use_reloader=False"