Attach a debugger to Azure Function App running in a container - python

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

Related

Use debugger in Visual Studio Code with Docker

I am working on a project where we use Docker to develop an application. Currently I'm running my programmes by typing docker-compose run --rm app in the console.
Then I can run my scrip like this root#ce4d1325fb94:/home/app# python3 example.py. But I would like to use my debugger. Normally I can use it be pressing F5.
I have found Debug containerized apps and Debug Python within a container.
But id does not work.
I get the error Could not find the task 'docker-run: debug' or ConfigError: The project 'Projekte_635c102' is not a valid java project.
What am I doing wrong? Note: I have no clue what Docker exactly is. I was told to use it.
I've done everything exactly as #MingJie-MSFT said. I’m still getting the same Could not find the task 'docker-run: debug' error. I can't create an example since I have no clue what I'm doing. I never worked with Docker or got an explanation on how to use it. I was just told to use the existing code. I can give you the Dockerfile if that is of any help:
# syntax=docker/dockerfile:1
FROM ubuntu:latest
WORKDIR /home/app
# Pass 8 and 63 to tzdata to set up the default time zone
RUN apt-get update && echo 8 63 | apt-get install -y tzdata
RUN apt-get install -y python3-pip libgdal-dev gdal-bin default-jre && pip3 install -r requirements.txt
I also tried to just type docker-run: debug in my console while being in the container.
root#cd70584fb560:/home/app# docker-run debug
and I've got
bash: docker-run: command not found
I've tried to include a "tasks" in my launch.json file. But again no luck. I thought maybe it's trying to find the "preLaunchTask" specified in the launch.json file before. Again I am absolutely clueless here. How is this launch.json file supposed to work? What are those commands supposed to do? I only know a bit of Python and barely anything about using command-line commands.
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
],
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"python": {
"args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
"file": "manage.py" // how to I change this to current file?
}
}
]
}
Read the documentation about debug in Docker.
ConfigError: The project 'Projekte_635c102' is not a valid java project.
It identifies your project as a Java project. It seems that your launch.json file has an error. Check your launch.json file. It should look like this:
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}

Debugging dockerized Django in VS Code results in error "Timed out waiting for launcher to connect"

I want to debug my Django application using a docker container in Visual Studio Code.
Microsoft published a guide how to do that, which I followed step by step:
https://code.visualstudio.com/docs/containers/quickstart-python
But when I try to run the debugger, I get the following error message:
Timed out waiting for launcher to connect
Here is what I did step by step:
I initialized a simple Django application using django-admin startproject helloworld
In VS Code I opened the folder including the manage.py
Opened Command Palette Ctrl + Shift + P, and then selected Docker: Add Docker Files to Workspace...
Select Application Platform Python: Django
Include Docker Compose files No
Relative path to the app's entrypoint manage.py
What ports does your app listen on? 8000
VS Codes then creates several files (see below).
When I try to start the debugger (like in the guide), I get the following error message:
The terminal doesn't show any error messages, but the commands executed:
.vscode/launch.json:
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}
.vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "dockerdebugging:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"args": [
"runserver",
"0.0.0.0:8000",
"--nothreading",
"--noreload"
],
"file": "manage.py"
}
}
]
}
Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1
# Install pip requirements
ADD requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
ADD . /app
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "helloworld.wsgi"]
requirements.txt:
# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file
django==3.0.3
gunicorn==20.0.4
VS Code Version: 1.47.1
Python Extension Version: v2020.7.94776
The idea of debugging in vs code is to:
use debugpy to launch your code with a port, say 5678
in vscode to 'Remote Attach' to that process.
I may be wrong but what i can see from your code, you didn't attach to your process.
I wrote the way I did here, I use docker-compose and docker. My way is different with yours but you will get the idea...:)
My issue were missing packages. Docker usually works fine, I haven't had any issues before at all.
I originally installed docker like described in the official documentation:
https://docs.docker.com/engine/install/ubuntu/
But after I tried installing the docker.io package, debugging worked fine in VS Code:
sudo apt install docker.io
There's no errors compared to the vscode tutorial in your project. Cause the error is timeout waiting for luncher to connect, try restart docker service and reload your window in vscode.
Here is a clue and a workaround:
The instructions I followed had me open VS code inside my ubuntu WLS2 instance. Note: my app is just a generic python app, not django.
If I click this and change it to open as a windows folder, then fun debug, everything suddenly works for me. (It spins up the docker and connects to it with debug, does breakpoints etc.)
I think what is happening for me is that "sometimes" docker is starting the container/app on its own WSL instance and my ubuntu instance cannot route to it for the debug.
I say sometimes, because sometimes it works. I think it might be related to which application (docker, ubuntu, vscode) I start first when I boot my machine.
I've messed with the docker, resources, WSL integration settings, the windows firewall, and restarted various things.
I'm sure a proper fix is not that complicated, however running VS code in native windows is enough of a fix for me. I don't see why the added complexity of starting the VS code session inside WSL is actually necessary.

Remote debugging Docker container with ptvsd doesn't stop on breakpoint

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')

VScode: How to debug a python 2.7 cgi script?

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.

Python remote debuging with PyCharm and ptvsd

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"

Categories