j2eeexpert2015 / springbootdebug

2 stars 14 forks source link

python debug with docker-compose in vscode? #1

Open yezhengli-Mr9 opened 10 months ago

yezhengli-Mr9 commented 10 months ago

Dear @j2eeexpert2015, While, thanks for the video

  1. Java Debugging in Docker: How to Use VS Code and Docker Compose and
  2. Debug Flask inside Docker Container using docker compose with pycharm 2021.3.3, etc. https://github.com/j2eeexpert2015/flask_docker/issues/1

Is there any tutorial for python debug with docker-compose in vscode similar to https://medium.com/@lassebenninga/how-to-debug-flask-running-in-docker-compose-in-vs-code-ef37f0f516ee? Problem with 2 is: have to use docker-compose up for sudo su -- tried following for https://github.com/j2eeexpert2015/flask_docker/issues/1 but does not seem work (1) I set run python with sudo privileges in Python interpreter according https://youtrack.jetbrains.com/issue/PY-15288/Make-it-possible-to-run-remote-interpreter-as-root image

(2) try run some script similar to https://esmithy.net/2015/05/05/rundebug-as-root-in-pycharm/ (or mentioned in https://youtrack.jetbrains.com/issue/PY-15288/Make-it-possible-to-run-remote-interpreter-as-root as well) for image Best and thanks,

yezhengli-Mr9 commented 10 months ago

From query like https://www.youtube.com/@learningfromexperience/search?query=python%20docker%20compose%20vscode do not find relevant video.

yezhengli-Mr9 commented 10 months ago

Video Java Debugging in Docker: How to Use VS Code and Docker Compose relevant to spring boot can definitely get debugged even just with DockerFile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAVA_OPTS
ENV JAVA_OPTS=$JAVA_OPTS
# ENV JAVA_TOOL_OPTIONS -agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n
COPY target/springbootdebug-0.0.1-SNAPSHOT.jar springbootdebug.jar
EXPOSE 8080
EXPOSE 5005
CMD ["java", "-jar", "springbootdebug.jar"]
yezhengli-Mr9 commented 10 months ago

By referring to (1) Java Debugging in Docker: How to Use VS Code and Docker Compose (2) https://medium.com/@lassebenninga/how-to-debug-flask-running-in-docker-compose-in-vs-code-ef37f0f516ee feel at least two points are important to debug in vscode, specifically for python with docker-compose (3) https://code.visualstudio.com/docs/containers/docker-compose for python

  1. launch.json
    {
    // 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: Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        }
    ]
    }

    compared to here

    {
    // 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": [
        {
            "type": "java",
            "name": "Debug in Docker",
            "request": "attach",
            "hostName": "localhost",
            "port": "5005"
        }
    ]
    }
  2. DockerFile in (2) https://github.com/cookiecutter-flask/cookiecutter-flask/issues/1966
    services:
    flask-dev:
    build:
      context: .
      target: development
      args:
        <<: *build_args
    image: "my_flask_app-development"
    ports:
      - "8080:5000"
      - "2992:2992"
      - "5678:5678"
    <<: *default_volumes

    While (2) has more steps in Dockerfile

    RUN pip install debugpy

    and Add the debugpy command to the launch script and package.json

    "scripts": {
    "start": "python -m debugpy --wait-for-client --listen 0.0.0.0:5678 -m flask run --host=0.0.0.0"
    },

    which coincides with hints in (3).

yezhengli-Mr9 commented 10 months ago

One attempt is described in https://github.com/microsoft/debugpy/issues/1491 as well as https://github.com/microsoft/debugpy/issues/1296 by following https://code.visualstudio.com/docs/containers/docker-compose