Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remote debug python code in a Docker Container with VS Code

Tags:

I've just registered for this question. It's about if it's possible to remote debug python code in a Docker Container with VS Code. I am having a completely configured Docker Container here. I got a little bit of help with it, and I'm pretty new to docker anyways. In it runs Odoo v10. But I cant get the remote debug in VS Code to work. I have tried this explanation, but I don't really get it. Is it even possible? And if yes, how can I get it to work? I'm running Kubuntu 16.04 with VS Code 1.6.1 and the Python Extension from Don Jayamanne. Ah yeah and I hope I am at the right location with this question and it's not against any rules.

UPDATE:

Just tried the way of Elton Stoneman. With it I'm getting this error:

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

My Dockerfile looks like this:

FROM **cut_out** USER root # debug/dev settings  RUN pip install \         watchdog  COPY workspace/pysrc /pysrc RUN apt-get update \  && apt-get install -y --no-install-recommends \         build-essential \         python-dev \  && /usr/bin/python /pysrc/setup_cython.py build_ext --inplace \  && rm -rf /var/lib/apt/lists/*  EXPOSE 3000  USER odoo 

The pysrc in my Dockerfile is there because this was intended for working with PyDev (Eclipse) before.

This is the run command I've used:

docker-compose run -d -p 3000:3000 odoo 

And this is the important part of my launch.json:

    {         "name": "Attach (Remote Debug)",         "type": "python",         "request": "attach",         "localRoot": "${workspaceRoot}",         "remoteRoot": "${workspaceRoot}",         "port": 3000,         "secret": "my_secret",         "host": "172.21.0.4"     } 

I hope that's enough information for now.

UPDATE 2:

Alright I found the solution. I totally misunderstood how Docker works and tried it completeley wrong. I already had a completeley configured Docker-compose. So everything I needed to do was to adapt my VS Code configs to the docker-compose.yml. This means that I just had to change the launch.json to the port 8069 (default Odoo port) and just need to use docker-compose up, then the debugging works in VS Code. Unfortunately the use of ptvsd kinda destroys my Odoo environment, but at least I'm able to debug now. Thanks!

like image 275
Brotbret Avatar asked Oct 25 '16 07:10

Brotbret


People also ask

How do I debug python code in VS Code?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.


2 Answers

Yes, this is possible - when the Python app is running in a Docker container, you can treat it like a remote machine.

In your Docker image, you'll need to make the remote debugging port available (e.g. EXPOSE 3000 in the Dockerfile), include the ptvsd setup in your Python app, and then publish the port when you run the container, something like:

docker run -d -p 3000:3000 my-image 

Then use docker inspect to get the IP address of the running container, and that's what you use for the host in the launch file.

like image 103
Elton Stoneman Avatar answered Sep 23 '22 11:09

Elton Stoneman


works with vscode 1.45.0 & later. for reference files https://gist.github.com/kerbrose/e646aaf9daece42b46091e2ca0eb55d0

1- Edit your docker.dev file & insert RUN pip3 install -U debugpy. this will install a python package debugpy instead of the deprecated one ptvsd because your vscode (local) will be communicating to debugpy (remote) server of your docker image using it.

2- Start your containers. however you will be starting the python package that you just installed debugpy. it could be as next command from your shell.

docker-compose run --rm -p 8888:3001 -p 8879:8069 {DOCKER IMAGE[:TAG|@DIGEST]} /usr/bin/python3 -m debugpy --listen 0.0.0.0:3001 /usr/bin/odoo --db_user=odoo --db_host=db --db_password=odoo 

3- Prepare your launcher file as following. please note that port will be related to odoo server. debugServer will be the port for the debug server

{     "name": "Odoo: Attach",     "type": "python",     "request": "attach",     "port": 8879,     "debugServer": 8888,     "host": "localhost",     "pathMappings": [         {             "localRoot": "${workspaceFolder}",             "remoteRoot": "/mnt/extra-addons",         }     ],     "logToFile": true } 
like image 41
kerbrose Avatar answered Sep 20 '22 11:09

kerbrose