Odoo in a root-less container

The main workstation running Fedora 31 now, devoid of any trace of python2, I had to either spin up a virtual machine (which I happily did in the past using qemu and kvm [no libvirt or GNOME Boxes]) or get the hands dirty on containers this time to develop on Odoo [1] version 10 which depends on python2. Faced with the challenge^Wopprotunity, I started to learn to use containers.

Never tried to use docker, even though I am familiar with its technology and at times I wanted to try and have hands on experience on the technology. Fast forward, podman and buildah came along with the possibility to run root-less containers and they’re available in Fedora.

Steps

Install and setup podman, optionally buildah. Consult documentation at Red Hat developer blog [2] posts [3].

#Install
$ su -c "dnf install -y podman buildah
#Make sure your user is present in subuid and subgid
$ su -c "usermod --add-subuids 10000-75535 $(whoami); \
  usermod --add-subgids 10000-75535 $(whoami)"
#Logout and login back for the normal user to take effect.

Setup and run postgresql using podman. The documentation [4] on docker hub and at Red Hat [5] will help, also Dan Walsh’s post [6]. You’d want persistent storage for database and the application.

#Create a persistent storage location for DB
$ su -c "mkdir -p /var/container/pgsql10/data"
#Make sure to give ownership of the directory to 'postgres' user
#in _container_. In host system, id of 'postgres' user is '26',
#which maps to id '10025' in container. 
$ su -c "chown -R 10025:10025 !$"  # directory created in previous step
#As normal user
$ podman run -d --name pg10 -e POSTGRESQL_USER=odoo \
  -e POSTGRESQL_PASSWORD=odoopassword -e POSTGRESQL_ADMIN_PASSWORD=postgrespassword \
  -e POSTGRESQL_DATABASE=postgres -p 9432:5432 \
  -v /var/container/pgsql10/data:/var/lib/pgsql/data \
  -m=1g rhscl/postgresql-10-rhel7
#Check logs
$ podman logs -f pg10
#Connect the database and grant privileges to 'odoo' user
$ psql -U postgres -h 192.168.1.100 -p 9342 -d postgres
password:
postgres=#ALTER USER odoo WITH CREATEDB;

These steps demand some comments. To setup persistent storage for database, create a directory and give ownership of that directory to the user in container (refer [6] for details) . The user id for chown should be the mapped id of user within the container. For example, if the id of postgres user is 25 in host system and the id of same user in container will usually be 10025.

Next you can pull the postgresql docker image and run it. The environment variables using -e option are passed into the container. The -p option creates a port mapping between host and container. The -v option provides volume (persistent storage) mapping between host directory and container directory. The -m option provides memory restrictions required for postgres to auto-tune. If everything goes well, container named pg10 is created and run as daemon process. Check the status using podman ps -a or logs using podman logs -f pg10.

We are running postgresql as user odoo and this user should be able to create databases. Login to the database using super user postgres and the password specified in POSTGRESQL_ADMIN_PASSWORD, connecting to the IP address of host machine (localhost doesn’t work) on host port 9432. Then grant CREATEDB privilege to the odoo user.

Once postgresql is running successfully, let us create another container to run odoo. We also want odoo container to use the database server running in pg10 container, connected using same POSTGRESQL_USER user. There are couple of ways to connect to another container — one is using host port mapping and the other is using pod, see [7] for details. I chose the first option. Before running the container, create volume mapping for configuration and addons directories.

#Configuration
$ mkdir -p $HOME/odoo10_conf
$ cat > $HOME/odoo10_conf/odoo.conf << EOF 
[options]
; podman postgresql communication using port mapping
db_host = 192.168.1.100
db_port = 9432
db_user = odoo
db_password = odoopassword
addons_path = /mnt/extra-addons
data_dir = /var/lib/odoo
EOF
#Create and run odoo container
$ podman run -d -v $HOME/odoo10_conf:/etc/odoo \
  -v /opt/odoo/addons/odoo10:/mnt/extra-addons -p 9010:8069 \
  --name odoo10  odoo:10

We need to be able to control and pass the Odoo configuration from host system. Create a directory and place the configuration file in there and map it to /etc/odoo/odoo.conf in container. Similarly, you would do addons development in your host machine, so map the addons directory which is expected at /mnt/extra-addons by the container. The host port 9010 is mapped to container port 8069 used by Odoo.

That’s it.

Connect to Odoo by going to localhost:9010 and build your next application.

Oh — you can stop the container using podman stop pg10 and start using podman start odoo10 etc.

Update (15-Feb-2020)

What if you like to run both these containers in a pod to provide easier network access between them? This might be desired for various reasons, such as the IP address of the host machine changes and yet want to access the database from Odoo container without adjusting the IP of db_host.

The solution is to put both database and application (Odoo) containers in a single “pod”. A new pod can be created using podman pod create --infra -p hostport:containerport <podname> and while creating containers using podman run, pass the <podname> as podman run --pod <podname> .... It is important to specify all the ports you need to access from the host while creating this pod — it is not possible to add port mappings afterwards. Since I’d only need to access Odoo from the host, it would suffice to specify the port mapping -p 7069:8069. In short:

$ podman pod create --infra -p 7069:8069 --name odb
$ podman run -d --pod odb --name pg10 -e ...
$ # Make following changes to odoo.conf file
# db_host = localhost
# db_port = 5432
$ podman run -d --pod odb --name odoo10 ...

References

  1. Odoo, https://odoo.com/
  2. Red Hat developer blog, Introduction to podman, https://developers.redhat.com/blog/2018/08/29/intro-to-podman/
  3. Red Hat developer blog, Podman and buildah for docker users, https://developers.redhat.com/blog/2019/02/21/podman-and-buildah-for-docker-users/
  4. Docker hub, PosgreSQL 10 on CentOS 7, https://hub.docker.com/r/centos/postgresql-10-centos7
  5. Red Hat documentation, Software collections docker images — PostgreSQL, https://access.redhat.com/documentation/en-us/red_hat_software_collections/3/html/using_red_hat_software_collections_container_images/database-images#postgresql
  6. Dan Walsh, Does root-less podman make sense? https://www.redhat.com/sysadmin/rootless-podman-makes-sense
  7. Red Hat, Configuring container networking with podman, https://www.redhat.com/sysadmin/container-networking-podman

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: