Upgrade Official Odoo12 Image From py3.5 To py3.8

💡 This post is insightful for the following scenarios.

  • Python Upgrade
  • Docker Creation
  • Odoo Dependencies

Background

Due to the updated project requirements, we need to use new third-party libraries that require a newer version of Python. However, the Python version provided in the official Odoo 12.0 Docker image is too outdated, so we need to upgrade the Python version from 3.5 to 3.8.

Here I will use pyenv to upgrade the python version. And the final dockerfile will be offered in the end.

Environment: odoo:12.0 (link)

Step

1. Install the dependencies for python 3.8

1
2
3
# install the dependencies for python3.8
USER root
RUN apt-get update && apt-get install -y default-libmysqlclient-dev build-essential git gcc make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev libldap2-dev libsasl2-dev

2. Set the environment variable $PYENV_ROOT for pyenv

Reason: the docker will be run by user “odoo”. Therefore, we should set up a python path that can be accessed by user “odoo”

1
ENV PYENV_ROOT=/var/local/pyenv/

3. Install python 3.8 by pyenv

1
2
3
# install python 3.8
RUN curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
RUN /var/local/pyenv/bin/pyenv install 3.8.15 && /var/local/pyenv/bin/pyenv global 3.8.15

4. Change python 3.5 -> python 3.8 for odoo binary file

The way to test if the odoo can run in odoo offcial docker image, is to input “odoo” and click “enter” button.

How do you know which python it is using? Let try!

Well. Now is easy. Just create a soft link to current python3.

1
RUN rm /usr/bin/python3 && ln -s /var/local/pyenv/shims/python3 /usr/bin/python3

5. List all the python 3.5 package and try to install them into python 3.8 package

Use this command to see current package.

1
2
3
4
5
# check current package
pip3 list

# output current package as requirements.txt
pip3 freeze > requirements.txt

Here comes some principle that I follow:

  • For some packages you can not download it by pip, like odoo, you should completely move it from 3.5 package to 3.8 package, including package itself and its information (.egg-info file)
  • For some packages version you can directly download and use, you should download it to avoid unexpected error.
  • For some pacgage version you cannot download or cannot use, you should download the newest version.
1
2
3
4
5
6
# install odoo dependencies
RUN mv /usr/lib/python3/dist-packages/odoo /var/local/pyenv/versions/3.8.15/lib/python3.8/site-packages/odoo && mv /usr/lib/python3/dist-packages/odoo-12.0.post20211006.egg-info/ /var/local/pyenv/versions/3.8.15/lib/python3.8/site-packages/odoo-12.0.post20211006.egg-info/

# you should put dockerfile and requirements.txt in the same folder
COPY ./requirements.txt /
RUN /var/local/pyenv/shims/pip install -r /requirements.txt

6. Change to user “odoo”

If you forget to change current user, odoo will fail to start.

1
USER odoo

7. Build the docker image

1
docker build -t UserName/RepName:TagName .

Reference

Dockerfile download link

requirements.txt download link