Skip to content
Home » My Blog Tutorial » Debug Odoo Docker in VSCode

Debug Odoo Docker in VSCode

Enhancing IDE with Remote Debug : Full version

Introduction:

Welcome to tutorial Debug Odoo Docker in VSCode where you’ll learn to transform VSCode (Visual Studio Code) into a more powerful Integrated Development Environment (IDE) using Docker extensions and remote debugging.

Preparation VSCode and Docker

Before starting, ensure that Visual Studio Code and Docker already install on your PC. Begin by downloading the necessary source code from GitHub using this syntax in terminal / CMD :

The GitHub repository at teguhteja/docker-odoo-dev is a public repository fork from mjavint/docker-odoo-dev. It appears tailor for Odoo development using Docker in Visual Studio Code (VSCode), with a focus on providing intelligent completion for Odoo development. The repository includes a variety of files and configurations that are essential for setting up a development environment for Odoo, a popular open-source ERP system.

Key features of this repository include:

  • VSCode Integration: Instructions for installing VSCode and necessary extensions for Python and Odoo support.
  • Workspace Setup: Guidance on creating a workspace by importing Odoo source code.
  • Python Type Checking: Configuration of pyrightconfig.json for Python type checking in VSCode.
  • Docker Image Compilation: Utilization of a Dockerfile provided in the repository to compile a Docker image for Odoo development.
  • Environment Setup: Instructions for setting up environment variables in the .env file.
  • Debug Configuration: Details on configuring VSCode for efficient debugging of Odoo applications.

This repository is particularly useful for developers looking to streamline their Odoo development process using Docker and VSCode, offering a comprehensive guide and necessary tools for an efficient development setup.

Installing Extensions VSCode

Setting up a development environment in Visual Studio Code (VSCode) for Python and Odoo development involves a few essential steps to ensure a smooth and efficient workflow. Here’s a guide to get you start

  • Open Visual Studio Code.
  • Navigate to the Extensions panel.
  • Search and install the Python extension.
  • Look for and install framework integration extensions.
  • These extensions are crucial for a robust development environment.

Configuring Odoo Docker

In the first phase of our Docker configuration, we focus on the Docker build process. This is a crucial step in creating a Docker image that includes both Odoo, a comprehensive suite of business management software tools, and debugpy, a Python library for debugging. The key to this process is the modification of the requirement.txt file within the Docker image. By adding debugpy to this file, we embed essential debugging capabilities directly into the Docker environment. This integration is vital as it equips the Docker image with robust tools necessary for effective software development and problem-solving.

Dockerfile

Open the Dockerfile in Visual Studio Code.

This Dockerfile snippet is for creating a Docker image based on the Odoo 16.0 image. It starts by setting the user as ‘root’, which grants full access to the container. The COPY command then transfers a file named requirements.txt from the local directory to the root directory of the container. This file typically contains a list of Python packages required for the application. The RUN pip3 install -r /requirements.txt command installs these Python packages inside the container using pip, Python’s package installer. Finally, RUN rm /requirements.txt removes the requirements.txt file from the container, which is a common practice to keep the image size small by removing unnecessary files after they have been used.

Open the requirements.txt in Visual Studio Code

debugpy
pydevd-odoo

The requirements.txt file in this context contains two Python packages: debugpy and pydevd-odoo. debugpy is an open-source Python debugger designed for use with Visual Studio Code and other IDEs. It allows for interactive debugging, remote debugging, and multi-threaded and multi-process debugging, making it a versatile tool for Python development. On the other hand, pydevd-odoo is a specific debugger extension for Odoo, an open-source ERP and CRM platform.

Docker build

This package likely extends or enhances the debugging capabilities within the Odoo development environment, allowing developers to more effectively debug Odoo modules and applications. Including these packages in requirements.txt ensures that they are installed in the environment, providing necessary tools for development and debugging of Python applications, particularly those related to Odoo.

in terminal vs code running docker file using syntax :

The command docker build -t odoodev:16.0 . is used to build a new Docker image from a Dockerfile in the current directory (denoted by .). The -t flag assigns a tag to the image, in this case, odoodev:16.0, which helps in identifying the image with a name and a version. This command instructs Docker to read the Dockerfile in the current directory and execute the instructions written in it sequentially.

These instructions typically include setting up the base image, copying files into the image, setting environment variables, running commands, and exposing ports, among others. The end result of this command is a new Docker image named odoodev with the tag 16.0, which is stored locally on your machine. This image can then be used to create and run containers with the Odoo 16.0 environment and configurations specified in the Dockerfile. Waiting it until done and you have docker images odoo 16 contain debugpy

Setting Folder VSCode

Pyright is a static analysis tool for design to check for errors in Python code and support static typing. The pyrightconfig.json file is a key configuration for Pyright, allowing users to customize how Pyright interacts with their Python project. This file contains settings that determine how Pyright should process the code, including paths for modules and stubs.

If you open pyrightconfig.json and look at the content :

The contents of the pyrightconfig.json file you provided consist of two main parts:

  1. “stubPath”: This path points to the location of stubs. Stubs are files containing type declarations for external libraries. In this case, the "stubPath" is set to a directory containing stubs for the Odoo project, which is a suite of integrated business applications.
  2. “extraPaths”: This section includes a list of additional paths that Pyright should consider when analyzing the code. This allows Pyright to find and include modules located outside the standard directory. In this example, these paths lead to directories related to the Odoo project and custom addons associated with a Docker project.

By specifying these paths, Pyright can be more accurate in analyzing and providing feedback on Python code, especially in complex projects with many dependencies and custom modules.


Odoo-stubs

To obtain odoo-stubs16, which are essential for enhancing type checking and IntelliSense in Python projects using Odoo 16, you typically need to look for them in repositories that host Python stubs or type hints for various libraries. These stubs are often available on platforms like GitHub like ,

where developers and contributors create and maintain them for public use. You can search for odoo-stubs or odoo 16 stubs on GitHub or similar platforms. Once found, you can clone or download the repository and reference the local path in your pyrightconfig.json file under the "stubPath" key. This will enable Pyright to use these stubs for static type checking and autocomplete features in your Odoo 16 project. Remember to verify the compatibility and updates of the stubs with your specific version of Odoo to ensure accurate and effective type checking.


To acquire odoo16, the latest version of the Odoo ERP software, you should visit the official Odoo website or their GitHub repository.

Odoo provides a comprehensive suite of business applications and is known for its modularity and rich feature set. On the official Odoo website, you can find options to download the software, access documentation, and explore various services offered. For a more hands-on approach, especially if you’re looking to customize or develop with Odoo, visiting the Odoo GitHub repository is advisable.

Here, you can clone or download the source code of Odoo 16. This approach is particularly beneficial for developers who need to integrate Odoo into larger projects or require a deeper level of customization. Always ensure that you are downloading from the official sources to guarantee you are getting the most stable and secure version of the software.

Odoo.conf

Open file odoo.conf :

The odoo.conf file is a crucial configuration file for Odoo, an open-source suite of business applications. It defines various parameters that control how the Odoo server operates. The addons_path parameter specifies the directories where Odoo will look for additional modules or custom addons, enhancing the functionality of the base system. The admin_passwd is a security feature, setting the password for the administrative database operations.

Simple Addons Odoo

Next, create simple addons that debug, for example the write() function. For example, in the GitHub folder there is already an addon with the name om_hospital. Structure as in the picture.

This addon have structure in a typical Odoo module format, with key components organized in specific directories and files. The main elements of this addon include:

  1. Models Directory: This folder likely contains the Python models defining the data structure and business logic specific to the hospital management functionality.
  2. Static/Description Directory: This directory usually holds static files, such as images or descriptions, which are used for the module’s frontend or documentation.
  3. init.py File: A standard Python file used to treat the directory as a package, allowing its modules to be imported.
  4. manifest.py File: This file is crucial in Odoo modules as it contains metadata about the addon, such as its name, version, dependencies, and other necessary information for its integration into the Odoo system.

then the account_move.py file used as a debug sample contains code and a red mark for debug starting on that line

Remote Debugging Setup

Moving on to the next phase, we concentrate on setting up Docker containers using Docker compose. This step is essential for defining and running multi-container Docker applications, which is integral to a streamlined and efficient development process. The configuration of folders for Docker compose ensures that each component, including Odoo and the debugpy-enhanced environment, operates seamlessly within the Docker container. This organization and management of the development environment highlight the versatility and power of Docker, making it an indispensable tool for modern software developers.

In the project folder there is an .env file which contains the variables used in docker-compose.yaml

This configuration snippet outlines the setup for an Odoo environment, version 16.0 (ODOO_VERSION=16.0), with the container named o16ce. The default Odoo port is set to 8069 (ODOO_PORT_DEFAULT=8069), with a custom port at 8016 (ODOO_PORT_CUSTOM=8016). For debugging, the Debugpy port is configured to 8888 (DEBUGPY_PORT=8888). The PostgreSQL database, version 13.0-alpine (PG_VERSION=13.0-alpine), is running in a container named p13 (PG_CONTAINER_NAME=p13), with the user and password both set to odoo16 (PG_USER=odoo16, PG_PASSWORD=odoo16), and the default PostgreSQL port is 5432 (PG_PORT=5432). The Odoo server path is ./odoo16 (ODOO_SERVER=./odoo16), and the custom addons directory is ./custom_addons (CUSTOM_ADDONS=./custom_addons). The entry point for running Odoo is specified as a Python command (ENTRYPOINT=/usr/bin/python3 -m debugpy --listen 0.0.0.0:8888 /usr/bin/odoo -c /etc/odoo/odoo.conf -d odoo -i om_hospital), indicating the use of Debugpy for debugging and specifying the Odoo configuration file and database.

Docker-compose.yml

This Docker Compose configuration, version 3.1, defines two services: web for Odoo and postgres for the PostgreSQL database. The web service uses a container named as per the ODOO_CONTAINER_NAME environment variable, built from an image tagged odoodev:${ODOO_VERSION}. It depends on the postgres service and exposes two sets of ports, one for Odoo (${ODOO_PORT_CUSTOM}:${ODOO_PORT_DEFAULT}) and one for Debugpy (${DEBUGPY_PORT}:${DEBUGPY_PORT}). The volumes for this service include a configuration directory (./conf:/etc/odoo), data storage (odoo-web-data:/var/lib/odoo), the Odoo server directory (${ODOO_SERVER}:/var/lib/odoo/odoo), and a directory for custom addons (${CUSTOM_ADDONS}:/var/lib/odoo/custom_addons).

The entrypoint is defined by the ENTRYPOINT environment variable. The postgres service uses a container named as per PG_CONTAINER_NAME, built from an image tagged postgres:${PG_VERSION}. Its environment variables set the database name, user, and password, and specify the data directory. The PostgreSQL port is mapped to ${PG_PORT}:5432. Two volumes, odoo-web-data and odoo-db-data, are declared for persistent data storage.

.vscode files

This folder .vscode contains specific configurations for Visual Studio Code (VSCode). These configurations are tailor to optimize efficiency in developing Odoo applications, an integrated suite of business applications. Leveraging Docker, a containerization technology that enables the creation, deployment, and management of applications in isolated environments, the .vscode folder ensures that developers can work in a consistent and controlled setting, regardless of their physical location. This not only speeds up the development process but also reduces the likelihood of encountering issues related to environment discrepancies among different developers.

File launch.json

The launch.json file in the Docker Odoo development environment plays a crucial role in streamlining the debugging process, particularly for Python applications. This configuration file, designed for use with Visual Studio Code, specifies how the debugger should connect to and interact with the code. In this specific instance, the file defines a “Python: Remote Attach” configuration, enabling developers to attach the debugger to a Python process running in a remote environment, such as a Docker container. The configuration specifies the host (localhost) and port (8888), indicating where the debugger should connect.

The pathMappings section is particularly noteworthy; it maps the local development paths to their corresponding paths in the remote environment. This includes mapping the custom_addons and odoo16/addons directories from the local workspace to their respective locations within the Docker container. This setup ensures that the debugger accurately reflects the file structure and execution environment of the Odoo application, facilitating a more effective and intuitive debugging experience. The justMyCode attribute set to true further refines the debugging process by focusing only on the user-written code, excluding library code, for a more streamlined and focused debugging session.

Running and Debugging Odoo Docker

Starting Docker Compose: First, run docker compose-up in the terminal or CMD.

Alternatively, you can use the Visual Studio Code (VSCode) extension explorer. Right-click on the docker-compose.yml file and select ‘Compose Up’.

Container Creation: This action triggers Docker to create containers for the web application, which is Odoo 16, and the database, which is PostgreSQL 15.

Running the Containers: Once the container creation is complete, you can run the containers using the command docker run -t -d.

Alternatively, you can use the Docker extension in VSCode to start the project by clicking ‘start’.

Viewing Logs: To view the logs, you can use the Docker extension and select ‘docker logs’.

Accessing Odoo: Next, open your web browser and navigate to localhost:8016 to create a database in Odoo. Use the same password as the admin password in the odoo.conf file and create a database, for example, naming it ‘odoo’.

Database Processing: Odoo will then process the creation of the database. Enter the username and password you set earlier.

Debugging Process: For debugging purposes, you can click running debugging in vs code

and install the accounting process and wait for a while.

Debug Steps: After steps installation, you can continue with the write into invoice

and automatic appear debug in vs code after save a draft.

Conclusion:


Conclusion: Enhancing VSCode with Docker and Remote Debugging

This blog post provides a step-by-step guide to enhance Visual Studio Code (VSCode) using Docker and remote debugging. The aim is to transform VSCode into a more powerful and efficient Integrated Development Environment (IDE), particularly for Odoo development. Key highlights of the guide include:

  1. Preparation: Ensuring the installation of VSCode and Docker, and cloning the source code from GitHub.
  2. Extension Installation: Adding Python and Odoo extensions in VSCode to support development.
  3. Docker Configuration: Building a Docker image that includes Odoo and debugpy, facilitating the debugging process.
  4. Folder Setup: Using configuration files like pyrightconfig.json for better Python code analysis.
  5. Remote Debugging Setup: Setting up Docker containers with Docker Compose to enable effective debugging.
  6. Running and Debugging: Steps to run and debug applications using Docker and VSCode.

This guide is designed to help developers leverage Docker and remote debugging features in VSCode, enhancing efficiency and ease in application development, especially for those based on Odoo. By following these steps, developers can create a consistent and controlled development environment, accelerating the development process and reducing issues related to environment discrepancies.


Discover more from teguhteja.id

Subscribe to get the latest posts sent to your email.

1 thought on “Debug Odoo Docker in VSCode”

  1. Pingback: Odoo VSCode Ubuntu : Setting Up - teguhteja.id

Leave a Reply

Optimized by Optimole
WP Twitter Auto Publish Powered By : XYZScripts.com

Discover more from teguhteja.id

Subscribe now to keep reading and get access to the full archive.

Continue reading