问题
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.
回答1:
I made it work following the next steps:
- Start containers with
docker-compose up
ordocker run
- Access your odoo container with root permissions using
docker exec -it -u 0 "container name" /bin/bash
- Install ptvsd
pip3 install ptvsd
- 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()
- Exit container and restart using
docker-compose restart
ordocker restart "container name"
- Execute
docker inspect "container name"
and copy theIPAddress
under theNetworks
section - Open visual studio and create the
launch.json
, paste the container ip under thehost
field and theport
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": "."
}
]
}
]
}
- Click on start Debugging and you should be able to see odoo logs on the Debug console
Note: Be sure to expose port 3000 on container
回答2:
Check running containers and also locally running odoo instances somethings definitely running on that port
lsof -i :8069 >> Try running it with the port number you are using in place of 8069
docker ps >> To see all running containers further you can use pycham to run debug in container
[see also] (https://docs.docker.com/engine/reference/commandline/port/)
回答3:
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
来源:https://stackoverflow.com/questions/57626427/how-to-debug-odoo-in-docker