31

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!

3
  • Can you update your Update 2 with what you put in your launch.json? Also not sure what you meant when you said you're using docker-compose up. An explanation of your workflow would be appreciated :) Commented Dec 3, 2016 at 2:48
  • Unfortunately I don't have my launch.json for this project anymore, but all I changed in it was the port to "port": 8069. docker-compose is a tool that extends the functionality of docker. docker-compose up just starts all the found docker containers. I have to say that it still didn't work as intended, I never got remote debugging docker in VS Code to work tbh. If you get it to work please tell me :). Commented Dec 8, 2016 at 10:43
  • Hi - follow up question - why did changing the port to 8069 solve your issue? Do you have the original docker-compose file? Commented Sep 7, 2019 at 11:40

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much, this helped me a lot already! But one more thing: Do I have to import ptvsd in every .py script I am having? Because that would be a lot with Odoo. Is there a better way, like for example one import per project?
Just had time to try it your way, unfortunately it's not working. Everytime I try to debug I get this error in my debug console:
Sorry, just getting used to StackOverflow. 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} There was an error in starting the debug server. Error = {"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"172.21.0.4","port":3000} (I get the same output twice) Any Ideas maybe?
Can you update your question with your Dockerfile and docker run command?
EXPOSE doesn't actually expose the port (but -p does), so you can always leave that out if you don't feel like keeping it around for documentation purposes; docs.docker.com/engine/reference/builder/#expose
3

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
}

4 Comments

yes, this is still working. do you face any problem??
I'm using this to start my docker (zope based application) sudo docker run -it -p 8080:8080 -v ... /usr/bin/python3 -m debugpy I got /usr/bin/python3: No module named debugpy under Ubuntu
well, you just need to install debugpy module in your container. you could do it in multiple ways. if you run a public image. you log into such image using docker exec -it -u 0 /bin/bash then pip3 install debugpy. however if you have a docker file you could just add RUN pip3 install -U debugpy then rebuild the image.
2

If you want a nice step by step walkthrough of how to attach a remote debugger for VS code in a container you could check out the youtube video "Debugging Python in Docker using VSCode".

He also talks about how to configure the Docker file such that the container does not includes the debugger when run in production mode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.