I am trying to add to my launch configuration two tasks to automatically build run and remove a docker environment for my debug.
So far I was always able to debug my code, but I needed before to manually start the docker environment with a separate script.
Here my launch.json
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 8889
},
"preLaunchTask": "docker-compose up",
"postDebugTask": "docker-compose down",
"pathMappings": [
{
"localRoot": "<host_path_to_app>",
"remoteRoot": "/app/"
},
]
}
]
}
my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "docker-compose up",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true,
},
"files": [
"./docker-compose.yaml",
],
}
},
{
"label": "docker-compose down",
"type": "docker-compose",
"dockerCompose": {
"down": {
},
"files": [
"./docker-compose.yaml",
]
}
},
]
}
my docker file:
FROM python:3.10.7-slim-bullseye as base
RUN pip install debugpy
COPY ./app /app
WORKDIR /app
FROM base as debug
# CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:8889", "--wait-for-client", "main.py"]
CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:8889", "main.py"]
FROM base as prod
CMD ["python", "main.py"]
and finally my docker-compose file:
version: '3'
services:
scipy-notebook:
ports:
- "8889:8889"
volumes:
- "<local_path_to_app>:/app/"
- "<path_to_local_storage>:/mnt/permanent_storage/"
environment:
- 'PMT_STG_PATH=/mnt/permanent_storage/'
- PYTHONUNBUFFERED=1
build:
context: .
dockerfile: Dockerfile
target: debug
image: test_image:beta
The python application is irrelevant, but I tried the following:
the docker image can be built and run normally
I can debug the application from visual studio code if I first run the debug image, using the flag --wait-for-client for debugpy within the docker CMD, i.e. I can set breakpoints in my local mapping and normally debug
the docker-compose down and docker-compose up tasks seem to work properly
if I remove the --wait-for-client flag from the debugpy CMD, then the docker image is built, exposes the correct port, it runs with any command within my app (e.g. writing a file with timestamp in my local storage), and is teared down after the application is done. No breakpoint is hit at any point until the docker container is teared down
with the procedure from point 4., but with the --wait-for-client flag on, then the image is built, but the up process is stopped before the debugger makes it to attach.
What can I do to make it work? Is there anything conceptually wrong? From the documentation I could find mainly procedures to debug frameworks like flask or django, which are not relevant for my case.
Related
Try to debug the Python code in Container using VS Code Remote Container Extension.
After the docker build task, I got a "Debug Adapter Executable not provide" error. Where could be wrong?
launch.json
{
"configurations": [
{
"name": "Docker: Python - General",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "general"
}
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "py:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"file": "app.py"
}
}
]
}
Looks like it is resolved if you define a local python interpreter even if you want to use docker. Open a .py file and define it with the button right bottom.
I also encountered this problem. After trying a fresh install of the VS Code Docker extension, to no avail, I noticed the actually command being triggered by the "docker-run: debug" task in the tasks.json:
docker run -dt -P --name "loadtiktokcampaigns-dev" --label
"com.microsoft.created-by=visual-studio-code" -v
"/Users/tnightengale/.vscode/extensions/ms-python.python-
2022.12.0/pythonFiles/lib/python/debugpy:/debugpy:ro,z"
--entrypoint "python3" "loadtiktokcampaigns:latest"
The key here is the -v command that is attempting to mount the /debugpy files into the container from the VS Code Python extension files.
For some reason, those files were not actually located at that path on my Mac. Deleting the ms-python.python-2022.12.0/ folder and reinstalling the VS Code Python extension solved the issue for me.
I have made a simple Python file called index.py with this relevant piece of code:
def main():
server = HTTPServer(('127.0.0.1', PORT), HomeHandler)
print("Server online, listening for requests.\n")
server.serve_forever()
if __name__ == "__main__":
main()
I then installed the Docker extension and chose to add Docker files. I now have these files:
Dockerfile
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim
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
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 8000 --disabled-password --gecos "" 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 ["python", "index.py"]
And docker-compose.debug.yml:
version: '3.4'
services:
serverlessproject:
image: serverlessproject
build:
context: .
dockerfile: ./Dockerfile
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen localhost:8000 index.py "]
ports:
- 8000:8000
As well as two files the .vscode folder:
launch.json:
{
"configurations": [
{
"name": "Docker: Python - General",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "general"
}
}
]
And tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "serverlessproject:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"python": {
"file": "index.py"
}
}
]
}
When debugging index.py it will print and start serving as expected. However, the Docker container will not be available via 127.0.0.1:8000 - and inspecting the terminal shows the last command ran looks like:
docker exec -d projectname-dev python3 /debugpy/launcher 172.17.0.1:43423 -- index.py
I can't find where this IP and port are actually actually defined - it's definitely not using the port I exposed in the Dockerfile or the portranges defined in docker-compose.yml or docker-compose.debug.yml. I'm certain this is just a simple line of configuration I can add to one of the above files, but I'm having trouble finding documentation for this specific case - most docs assume I'm using Django, for example.
I am following a tutorial from visual studio code on creating a docker web app to run my python script in.
for your information it is visible in the following link:
https://code.visualstudio.com/docs/containers/quickstart-python
I followed the django tutorial before this on installing it correctly and tested it so that it works. After allowing vs code to create all my necessary files, i tried to run it via my launch.json file.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
},
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}
when i try this, i instantly get returned:
> docker build --rm --pull -f "H:\Dockerfile" --label "com.microsoft.created-by=visual-studio-code" -t "h:latest" "H:\" <
unable to prepare context: path "H:\"" not found
The terminal process failed to launch (exit code: 1).
I installed everything on my H drive, and after this error I added the H to my PATH by adding H:\ to it. But unfortunately the error persists.
does anyone have a clue on how I could solve this?
I'm trying to debug my app in a docker container. The app is written in paython 2.7 but VS Code tries to debug it with python 3. Therfore, it cannot resolve packages and throws execptions.
Any idea?
My configurataions:
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"dockerRun": {
"containerName": "account",
"image": "account:latest",
"env": {},
"volumes": [
{
"containerPath": "/code",
"localPath": "${workspaceFolder}"
}
],
"ports": [
{
"containerPort": 8086,
"hostPort": 8086
},
{
"containerPort": 8085,
"hostPort": 8085
}
]
},
"python": {
"args": ["--config=/code/config/account.conf.localstage"],
"file": "skoobe-accountd"
}
},
{
"label": "docker-build",
"type": "docker-build",
"dockerBuild": {
"context": "${workspaceFolder}",
"dockerfile": "${workspaceFolder}/Dockerfile.arm.dev",
"tag": "account:latest"
}
}
]
}
launch.json
{
"configurations": [
{
"name": "Debug Account",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
],
"projectType": "general"
}
}
]
}
settings.json
{
"python.linting.pylintEnabled": true,
"python.pythonPath": "/usr/bin/python"
}
Dockerfile
FROM python:2.7.18
ENV AWS_ACCESS_KEY_ID="xxx"
ENV AWS_SECRET_ACCESS_KEY="xxx"
ENV AWS_DEFAULT_REGION="xxx"
ENV SQS_QUEUE="xxx"
ENV SQS_CHANGE_QUEUE="xxx"
# RUN mkdir -p /root/.ssh && \
# chmod 0700 /root/.ssh
# RUN apt-get update && \
# apt-get install openssh-server -y
WORKDIR /code/
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
WORKDIR tools/
RUN python setup.py install
WORKDIR /code/
# RUN pip install -r requirements.txt
CMD ["python", "accountd", "--config=/code/config/account.conf.localstage"]
I found a solution after some investigation:
I modfied lunch.json to the following:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
]
}
]
}
Then I added the following lines into the entypoint Python file:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 5678))
After running the docker container, from VS code IDE, I could run in debug mode.
In Visual Studio Code I changed the Default Terminal to Command Prompt.
When I want to debug my Azure Function the following command is executed:
Executing task: .venv\Scripts\activate ; func host start <
However, I think it should put the cmd separator "&&" instead of ";".
Otherwise the func host start will not execute.
Executing task: .venv\Scripts\activate && func host start <
As far as I know it should auto-detect this.
Is there a way to manually change this?
My settings.json:
{
"terminal.integrated.automationShell.windows": "C:\\windows\\System32\\cmd.exe",
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
},
"C:\\windows\\System32\\cmd.exe": {
"path": "C:\\windows\\System32\\cmd.exe",
"args": [],
"icon": "terminal-cmd"
},
"JavaScript Debug Terminal": {
"extensionIdentifier": "ms-vscode.js-debug",
"icon": "debug",
"id": "extension.js-debug.debugTerminal",
"title": "JavaScript Debug Terminal"
}
}
}
Thanks in advance!
#########
SOLUTION:
I was able the change the separator by editing the tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd host start",
"type": "shell",
"dependsOn": "pip install (functions)",
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\activate && func host start"
},
"isBackground": true,
"problemMatcher": "$func-python-watch",
},
{
"label": "pip install (functions)",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": []
}
]
}
Afterwards in the launch.json, I set the preLaunchTask to "cmd host start".
Thank you ejizba. Posting your suggestion as an answer to help other community members.
"The debug configuration is specified in your tasks.json and launch.json files in the .vscode folder. The 'windows' 'command' properties default to a PowerShell separator (;), but you're welcome to switch it to a cmd separator (I believe that would just be &&) .
however, because I think we can potentially refactor the tasks.json so that it doesn't use terminal-specific separators and prevent similar issues in the future ."
For more information please refer this SO THREAD