Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug odoo in docker?

I am not able to debug odoo in docker container.

I am using Visual Studio Code and I have the following launch.json configuration.

{
   "version": "0.2.0",
   "configurations": [
       {
           "name": "Odoo 12",
           "type": "python",
           "request": "launch",
           "stopOnEntry": false,
           "pythonPath": "${config:python.pythonPath}",
           "program": "/usr/bin/odoo",
           "args": [
             "--config=/etc/odoo/odoo.conf"
           ]
       }
   ]
}

Everytime I start a debugger, this error occurs:

Exception in thread odoo.service.httpd:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3/dist-packages/odoo/service/server.py", line 410, in http_thread
    self.httpd = ThreadedWSGIServerReloadable(self.interface, self.port, app)
  File "/usr/lib/python3/dist-packages/odoo/service/server.py", line 136, in __init__
    handler=RequestHandler)
  File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 476, in __init__
    HTTPServer.__init__(self, (host, int(port)), handler)
  File "/usr/lib/python3.5/socketserver.py", line 440, in __init__
    self.server_bind()
  File "/usr/lib/python3/dist-packages/odoo/service/server.py", line 151, in server_bind
    super(ThreadedWSGIServerReloadable, self).server_bind()
  File "/usr/lib/python3.5/http/server.py", line 138, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.5/socketserver.py", line 454, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

Does anyone know what is wrong with my debug configuration?

Thanks!!

UPDATE

Perhaps some more information is necessary. I start the odoo-server with a docker-compose file and then with VSCode I remotely attach to that odoo-server. Restarting the server with the odoo-bin command works fine with VSCode terminal.

I have two docker containers running:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
4ac4a4c8481f        odoo:12.0           "/entrypoint.sh odoo…"   7 days ago          Up 4 minutes        0.0.0.0:8069->8069/tcp, 8071/tcp   odoo-docker_web_1
5910cce38985        postgres:10         "docker-entrypoint.s…"   7 days ago          Up 4 minutes        5432/tcp                           odoo-docker_db_1

And only odoo server is running on 8069:

odoo@4ac4a4c8481f:/mnt/extra-addons$ lsof -i :8069
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3   1 odoo    7u  IPv4  44986      0t0  TCP *:8069 (LISTEN)

So probably I am wrong using "launch" request type, because it tries to restart the server. I have also tested this configuration:

{
   "name": "Odoo 12 Attach",
   "type": "python",
   "request": "attach",
   "port": 8069,
   "host": "localhost",           
 },

but then the debugger is terminating immediately without error message.

like image 277
tisq Avatar asked Aug 23 '19 12:08

tisq


2 Answers

In order to understand how remote debugging works for all python services or apps which based on it such as Odoo, Flask, Django, Web2py or whatever. you have to understand three different concepts the docker container, the debugger, the python app server (in our case it's Odoo). so in many cases, when running Odoo from docker it is like the following image: enter image description here

and what you really need to be able to debug would be like the following image: enter image description here

please note the difference:

  • without debugging you have two ports, one internal the other is external which will pass http requests from the browser to Odoo & vice versa. however after debugging you have 4 ports, 2 of them for the http request & the other 2 for debugging information (which is based on json in our case) from Vscode to debugpy & vice versa (you could also use 2 ports by the way).
  • without debugging your entrypoint would be whats defined in the Dockerfile. with debugging, you modify the entrypoint to be debugpy. which will be responsible for running Odoo

czuniga, approach is really good & it is almost the same as following:

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 151
kerbrose Avatar answered Sep 22 '22 17:09

kerbrose


I made it work following the next steps:

  1. Start containers with docker-compose up or docker run
  2. Access your odoo container with root permissions using docker exec -it -u 0 "container name" /bin/bash
  3. Install ptvsd pip3 install ptvsd
  4. Update odoo addons/__init__.py file to enable ptvsd attachement,locate file in /usr/lib/python3/dist-packages/odoo/addons/__init__.py and add the following code at the end of the file:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 3000))

ptvsd.wait_for_attach()
  1. Exit container and restart using docker-compose restart or docker restart "container name"
  2. Execute docker inspect "container name" and copy the IPAddress under the Networks section
  3. Open visual studio and create the launch.json, paste the container ip under the host field and the port that was specified on the ptvsd attach method, ex. In my case the ip was 172.27.0.3
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 3000,
            "host": "172.27.0.3",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        }
    ]
}
  1. Click on start Debugging and you should be able to see odoo logs on the Debug console

enter image description here

Note: Be sure to expose port 3000 on container

like image 41
czuniga Avatar answered Sep 22 '22 17:09

czuniga