Dockerize Custom Odoo Application
Create odoo and PostgreSQL docker image for your custom modules
Docker-Compose.yml
services:
web:
build:
context: ./
dockerfile: dockerfile
depends_on:
- db
ports:
- "8099:8069"
- "8070:8070" # XML-RPC port
- "8072:8072" # Longpolling port
volumes:
- odoo-web-data:/var/lib/odoo
- ./odoo.config:/etc/odoo/odoo.conf
- ./custom_addons:/mnt/extra-addons # Adjusted to match Odoo's expected path
environment:
- ODOO_RC=/etc/odoo/odoo.conf # Point to your config file
networks:
- webnet
db:
image: postgres:12
ports:
- "6432:5432"
environment:
- POSTGRES_DB=your_db_name
- POSTGRES_PASSWORD=your_db_passwd
- POSTGRES_USER=your_db_user
- PGDATA=/var/lib/postgresql/data/pgdata
volumes:
- odoo-db-data:/var/lib/postgresql/data
networks:
- webnet
volumes:
odoo-web-data:
odoo-db-data:
networks:
webnet:
This docker-compose.yml
file defines a multi-container setup for running an Odoo application with a PostgreSQL database using Docker. Here's an explanation of the services, configuration, and how the file structure fits into this:
1. Service: web
Purpose: This is the Odoo service, responsible for running the Odoo instance.
Key configurations:
Build Context:
context: ./
: The Docker build context is set to the current directory.dockerfile
: dockerfile: Specifies that the Dockerfile used to build this service is located in the root directory and is nameddockerfile
.
Depends On:
db
: The Odoo container depends on the PostgreSQL container (db
) to ensure the database service is started before Odoo.
Ports:
8099:8069
: Exposes Odoo's default web interface on port8099
of your local machine, mapped to Odoo’s internal port8069
.8070:8070
: Maps the local port8070
to Odoo's XML-RPC port for external integrations.8072:8072
: Maps the longpolling port, typically used for live updates or chat.
Volumes:
odoo-web-data:/var/lib/odoo
: Stores Odoo's persistent data (database filestore, logs, etc.) in a Docker-managed volume../odoo.config:/etc/odoo/odoo.conf
: Maps a local configuration file(odoo.config)
into the container at Odoo's configuration file path(/etc/odoo/odoo.conf)
../custom_addons:/mnt/extra-addons
: Maps your localcustom_addons
directory to/mnt/extra-addons
in the container, which is Odoo's directory for external/custom modules.
Environment Variables:
ODOO_RC=/etc/odoo/odoo.conf
: Tells Odoo to use the configuration file located at/etc/odoo/odoo.conf
(which is mapped fromodoo.config
).
Networks:
webnet
: Connects theweb
service to a network shared with other services (in this case, thedb
service).
2. Service: db
Purpose: This is the PostgreSQL service, which acts as the database for the Odoo instance.
Key configurations:
Image:
postgres:12
: Specifies that this service uses the official PostgreSQL version 12 Docker image.
Ports:
6432:5432
: Maps the database's internal port5432
to port6432
on your local machine, allowing you to connect to PostgreSQL outside of the container.
Environment Variables:
POSTGRES_DB=your_db_name
: Defines the initial database name to be created asyour_db_name
.POSTGRES_PASSWORD=your_db_passwd
: Sets the password for theyour_passwd
PostgreSQL user.POSTGRES_USER=your_db_user
: Defines the PostgreSQL superuser asyour_db_user
.PGDATA=/var/lib/postgresql/data/pgdata
: Sets the location for PostgreSQL data storage.
Volumes:
odoo-db-data:/var/lib/postgresql/data
: Stores the PostgreSQL data in a Docker-managed volume to persist data between container restarts.
Networks:
webnet
: Connects thedb
service to the same network asweb
so Odoo can communicate with PostgreSQL.
3. Volumes
odoo-web-data
: A named volume to persist Odoo’s data, including the filestore, logs, etc.odoo-db-data
: A named volume to persist PostgreSQL's data, ensuring the database is retained even if the container is destroyed.
4. Networks
webnet
: A custom Docker network that allows containers to communicate with each other. Both the web (Odoo) and db (PostgreSQL) services are part of this network.
Explanation of Key Files:
odoo.config
: Your Odoo configuration file, containing settings like the database name, user, and ports.custom_addons/
: A directory where you place your custom Odoo modules.dockerfile
: The Dockerfile that builds the Odoo container.docker-compose.yml
: The file that orchestrates the Docker containers for Odoo and PostgreSQL.
This setup ensures that Odoo can run with custom modules and persistent data, connected to a PostgreSQL database.
FROM odoo:15
# Install system dependencies (if any)
# Copy requirements.txt file into the container
COPY ./requirements.txt ./requirements.txt
# RUN sudo apt-get install build-essential python3-dev python2.7-dev \
# libldap2-dev libsasl2-dev slapd ldap-utils tox \
# lcov valgrind
RUN pip install -r ./requirements.txt
RUN pip install psycopg2-binary
# Set the custom addons directory
ENV ADDONS_PATH=/mnt/extra-addons
# Copy your custom module to the specified addons directory
COPY ./custom_addons ./mnt/extra-addons
RUN chmod -R 777 ./mnt/extra-addons
# Start Odoo
CMD ["odoo", "-c", "/etc/odoo/odoo.conf"]
Dockerfile
This Dockerfile defines the steps to create a custom Odoo 15 Docker image that includes additional dependencies and custom modules. Here's an explanation of each section:
1. Base Image
FROM odoo:15
Purpose: This line sets the base image for your Docker build. In this case, it uses the official Odoo 15 Docker image, which includes everything needed to run Odoo.
2. Copy
requirements.txt
and Install Python Dependencies# Copy requirements.txt file into the container COPY ./requirements.txt ./requirements.txt
Purpose: This command copies the
requirements.txt
file from your local project directory into the container. This file usually lists all the Python dependencies that need to be installed for your custom modules or the Odoo instance to function properly.RUN pip install -r ./requirements.txt
Purpose: This line runs
pip
inside the container to install all the Python packages listed inrequirements.txt
.RUN pip install psycopg2-binary
- Purpose: This command installs
psycopg2-binary
, a PostgreSQL adapter for Python, required for connecting Odoo to the PostgreSQL database.
3. Set the Custom Addons Directory
ENV ADDONS_PATH=/mnt/extra-addons
Purpose: This sets the environment variable
ADDONS_PATH
to/mnt/extra-addons
, where custom Odoo modules will be stored and searched by Odoo when starting up.4. Copy Custom Addons
# Copy your custom module to the specified addons directory COPY ./custom_addons ./mnt/extra-addons
- Purpose: This command copies the
custom_addons
directory from your local machine into the container at the/mnt/extra-addons
path. This is where your custom Odoo modules will be placed, making them available to Odoo during runtime.
- Purpose: This command copies the
5. Set Permissions for Custom Addons
RUN chmod -R 777 ./mnt/extra-addons
- Purpose: This sets read, write, and execute permissions (777) on the /mnt/extra-addons directory and its contents. This ensures that Odoo has the necessary access to the custom modules during runtime.
6. Start Odoo
CMD ["odoo", "-c", "/etc/odoo/odoo.conf"]
File Structure
/project-root
│
├── custom_addons/ # Custom Odoo modules directory
│ └── <custom module 1>
│ └── <custom module 2>
│
├── odoo.config # Odoo configuration file
│
├── dockerfile # Dockerfile for building the Odoo container
│
└── docker-compose.yml # Docker Compose file defining your services
Subscribe to my newsletter
Read articles from Vinayak Zutti directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by