Containerize your Odoo application with Docker

Containerize your Odoo application with Docker

Docker allows you to package your Odoo application with its dependencies into a lightweight, portable container. This container isolates Odoo’s processes and resources, ensuring consistent behavior regardless of the underlying environment. Compared to traditional deployments, Docker containers offer:

Portability: Run Odoo seamlessly across different machines (Windows, macOS, Linux) without worrying about environment conflicts. 

Resource Efficiency: Containers share the host operating system’s kernel, making them lighter and faster to start than virtual machines. 

Isolation: Each Odoo instance runs in its own container, preventing conflicts with other applications sharing the system.

Uninstall old versions

Before you can install Docker Engine, you need to uninstall any conflicting packages.

for pkg in docker.io docker-doc docker-compose docker-compose-v2
podman-docker containerd runc; do sudo apt-get remove $pkg; done

Step 1 – Installing Docker on Ubuntu 22.04 LTS

Installation

Install using the apt repository:

Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

$ sudo apt-get update

$ sudo apt-get install ca-certificates curl

$ sudo install -m 0755 -d /etc/apt/keyrings

$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

$ sudo chmod a+r /etc/apt/keyrings/docker.asc

echo \
“deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo “$VERSION_CODENAME”) stable” | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get install docker-ce docker-ce-cli containerd.io
docker-compose-plugin docker-compose

$ sudo apt-get update

$ docker –version

Step 2 - Installing Docker Compose:

To install the docker-compose command line tool, refresh your package list, then install the package using apt:

$ sudo apt update
$ sudo apt install docker-compose

You can confirm that the package is installed by running the following command:

$ docker-compose –version 

Step 3 — Running Odoo and PostgreSQL with Docker Compose:

Run the following commands to create the directory and then cd into it:

$ mkdir -p ~/docker/odoo
$ cd ~/docker/odoo

Now open a new blank YAML file called docker-compose.yml using nano or your preferred editor:

$ nano docker-compose.yml

Add the following lines to the file:

version: ‘3.9’
services:
  web:
    image: odoo:17.0
env_file: myenvfile.env
    depends_on:
    – db
    ports:
    – “8069:8069”
    volumes:
    – odoo-web-data:/var/lib/odoo
  db:
    image: postgres:14
    env_file: myenvfile.env
    volumes:
      – odoo-db-data:/var/lib/postgresql/data/pgdata
volumes:
  odoo-web-data:
  odoo-db-data:
The file defines two services. The first is called odoo, which runs the Odoo application. The second is called postgres, which is the PostgreSQL database container. Both services reference named volumes that they use to store data outside of the running container instances. Finally, the odoo service exposes port 8069 on your server to the Odoo container that is running on the same port 8069.
 
Save and exit the file when you are done editing it. If you are using nano, press CTRL+O then RETURN to save, then CTRL+X to exit.
 
The Odoo and PostgreSQL containers use environment variables to configure themselves. The docker-compose.yml file specifies the env_file directive for both services. This provides some level of security by keeping passwords out of the docker-compose.yml file, in case of using any version-control system like git. This approach is generally recommended instead of adding environment variables to the docker-compose.yml file directly, sinceit is a good practice to keep passwords out of your docker-compose.yml file. This approach is especially applicable if you’ll be committing your files to a Git repository or another source control system.

Create a ‘myenvfile.env’ file:

$ nano myenvfile.env

Add these environment variables to it.

#postgresql env variables
POSTGRES_DB=postgres #name of the PostgreSQL db that will be created or used by the PostgreSQL server.
POSTGRES_PASSWORD=odoo17 #db_user
POSTGRES_USER=odoo17 #db_password
PGDATA=/var/lib/pgsql/data/pgdata #storage location of DBs and data files of PSQL
# odoo env variables
 HOST=db
USER=odoo17
PASSWORD=odoo17

You’re now ready to start the odoo and postgres containers with the docker-compose command:

docker-compose up –d

The up sub-command tells docker-compose to start the containers and the associated volumes and networks that are defined in the docker-compose.yml file. The -d flag (which stands for “daemonize or detached ”) tells docker-compose to run the containers in the background so the command doesn’t take over your terminal.

When that’s done, Odoo should be running. You can test that a webserver is running at 127.0.0.1:8069(Server IP Address) or localhost://8069

If you would like to stop your Odoo and PostgreSQL containers at any time, run the following command in your ~/docker/odoo directory:

docker-compose down

We’re thrilled to announce upcoming blog posts that will guide you through Odoo deployment and automation!

  • Effortless Odoo with Custom Addons via Docker: Explore how to leverage Docker containers for streamlined Odoo deployment, including the seamless integration of your custom addons.
  • Automate Your Odoo Journey: CI/CD for Containerized Odoo Applications: Discover the power of CI/CD pipelines to automate the containerization and deployment process for your custom Odoo applications, ensuring efficiency and consistency.

Leave a Comment

Your email address will not be published. Required fields are marked *