国内如何用odoo源码制作docker镜像并通过docker compose部署

最近我一直在研究如何部署odoo的Saas,发现现在很多Saas服务是通过docker来进行部署和维护,包括后期的扩容和迁移都非常方便。所以我也就开始研究如何通过docker来部署odoo。

由于我使用的服务器在国内,在部署的过程中出现了很多问题。需要把源切换到国内的源或者下载到本地。在这个过程中踩了很多坑,在这里做一个记录。

首先需要自己手动下载的有:

  1. odoo源码 https://github.com/odoo/odoo
  2. wkhtmltox https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.buster_amd64.deb
  3. https://github.com/odoo/docker 里的entrypoint.sh odoo.conf wait-for-psql.py 这3个文件

docker和docker compose的安装请自行解决,这里就不赘述了。

$ sudo mkdir -p /opt/odoo && cd /opt/odoo
#在opt下创建odoo目录并进入odoo目录,将下载的文件上传到odoo目录里
$ sudo mkdir data/{web,db} etc log addons
$ sudo mv odoo.conf etc/odoo.conf
$ sudo unzip odoo-15.0.zip odoo15
#将下载的odoo源码解压到odoo15文件夹
$ sudo chown -R [youruser]:docker /opt/odoo
#修改这个目录及以下目录和文件的用户权限一定要分配给docker组
$ vi Dockerfile #创建Dockerfile文件把如下内容复制进去
#Dockerfile文件开始
FROM debian:buster-slim

SHELL ["/bin/bash", "-xo", "pipefail", "-c"]

# Generate locale C.UTF-8 for postgres and general locale data
ENV LANG C.UTF-8

COPY ./wkhtmltox_0.12.5-1.buster_amd64.deb ./
RUN sed -i s@/deb.debian.org/@/mirrors.163.com/@g /etc/apt/sources.list
RUN sed -i s@/security.debian.org/@/mirrors.163.com/@g /etc/apt/sources.list

# Install some deps, lessc and less-plugin-clean-css, and wkhtmltopdf
RUN apt-get update && \
    apt-get install -yq --no-install-recommends \
        libfreetype6-dev \
        libxml2-dev \
        libzip-dev \
        libldap2-dev \
        libsasl2-dev \
        libjpeg-dev \
        #libjpeg8-dev \ 这个一直安装失败,还没找到原因
        zlib1g-dev \
        libpq-dev \
        libxslt1-dev \
        libldap2-dev \
        libtiff5-dev \
        libopenjp2-7-dev \
        liblcms2-dev \
        libwebp-dev \
        libharfbuzz-dev \
        libfribidi-dev \
        libxcb1-dev \
        ca-certificates \
        curl \
        dirmngr \
        fonts-noto-cjk \
        gnupg \
        libssl-dev \
        node-less \
        npm \
        python3-venv \
        python3-wheel \
        python3-dev \
        python3-num2words \
        python3-pdfminer \
        python3-pip \
        python3-phonenumbers \
        python3-pyldap \
        python3-qrcode \
        python3-renderpm \
        python3-setuptools \
        python3-slugify \
        python3-vobject \
        python3-watchdog \
        python3-xlrd \
        python3-xlwt \
        xz-utils \
        build-essential \
    && apt-get install -y --no-install-recommends ./wkhtmltox_0.12.5-1.buster_amd64.deb \
    && apt-get -y autoremove \
    && rm -rf /var/lib/apt/lists/* wkhtmltox_0.12.5-1.buster_amd64.deb

# install latest postgresql-client
RUN echo 'deb http://mirrors.aliyun.com/postgresql/repos/apt/ buster-pgdg main' > /etc/apt/sources.list.d/pgdg.list \
    && GNUPGHOME="$(mktemp -d)" \
    && export GNUPGHOME \
    && repokey='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8' \
    && gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "${repokey}" \
    && gpg --batch --armor --export "${repokey}" > /etc/apt/trusted.gpg.d/pgdg.gpg.asc \
    && gpgconf --kill all \
    && rm -rf "$GNUPGHOME" \
    && apt-get update  \
    && apt-get install --no-install-recommends -y postgresql-client \
    && rm -f /etc/apt/sources.list.d/pgdg.list \
    && apt-get -y autoremove \
    && rm -rf /var/lib/apt/lists/*

# Install Node
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get update && \
    apt-get install -y nodejs && \
    apt-get -y autoremove && \
    rm -rf /var/lib/apt/lists/*
RUN npm config set registry https://registry.npm.taobao.org

# Install rtlcss (on Debian buster)
RUN npm install -g rtlcss \
    && rm -Rf ~/.npm /tmp/*

RUN useradd --create-home --home-dir /opt/odoo15 --no-log-init odoo

USER odoo
WORKDIR /opt/odoo15

# Install Odoo and dependencies from source and check out specific revision
COPY --chown=odoo:odoo odoo15 ./
RUN /bin/bash -c "mkdir -p /opt/odoo15/{etc,data,log,extra-addons}"
ENV PATH="/opt/odoo15/.local/bin:${PATH}"
RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
    pip3 install --upgrade pip && \
    pip3 install setuptools wheel && \
    pip3 install --no-cache-dir --upgrade -r requirements.txt && \
    pip3 install psycopg2-binary && \
    rm -Rf /var/lib/apt/lists/* /tmp/*

User 0
# Define runtime configuration
COPY --chown=odoo:odoo etc/odoo.conf /opt/odoo15/etc/odoo.conf
COPY --chown=odoo:odoo ./entrypoint.sh /entrypoint.sh
COPY --chown=odoo:odoo ./wait-for-psql.py /usr/local/bin/wait-for-psql.py
RUN chmod +x /entrypoint.sh && \
    chmod +x /usr/local/bin/wait-for-psql.py

EXPOSE 8069 8071 8072 
#这里是声明容器运行时提供服务的端口,官方的Dockerfile文件里是这样我也就没改
#以前14版只需要8069和8072,也许15版会用到8071还没研究
ENV ODOO_RC /opt/odoo15/etc/odoo.conf

USER odoo

ENTRYPOINT ["/entrypoint.sh"]
CMD ["odoo"]
#Dockerfile文件结束
#esc :wq保存退出
$ vi entrypoint.sh
#按照这个修改entrypoint.sh文件最下面,将odoo替换为odoo-bin的绝对路径
case "$1" in
    -- | odoo)
        shift
        if [[ "$1" == "scaffold" ]] ; then
            exec /opt/odoo15/odoo-bin "$@"
        else
            wait-for-psql.py ${DB_ARGS[@]} --timeout=30
            exec /opt/odoo15/odoo-bin "$@" "${DB_ARGS[@]}"
        fi
        ;;
    -*)
        wait-for-psql.py ${DB_ARGS[@]} --timeout=30
        exec /opt/odoo15/odoo-bin "$@" "${DB_ARGS[@]}"
        ;;
    *)
        exec "$@"
esac
#保存退出
$ vi etc/odoo.conf
#修改下面几处
[options]
addons_path = /opt/odoo15/extra-addons
data_dir = /opt/odoo15/data
admin_passwd = 管理密码这里自己设置
logfile = /opt/odoo15/log/odoo.log
logrotate = True
#并保存退出
$ vi docker-compose.yml
#docker-compose.yml文件开始
version: '3.1'
services:
  web:
    build: .
    depends_on:
      - db
    restart: always
    ports:
      - "8069:8069" #这里是把容器的端口映射本机的端口可以根据自己的实际情况设置
      - "8071:8071"
      - "8072:8072"
    volumes:
      - ./data/web:/opt/odoo15/data
      - ./etc:/opt/odoo15/etc/
      - ./log:/opt/odoo15/log
      - ./odoo15/odoo:/opt/odoo15/odoo
      - ./addons:/opt/odoo15/extra-addons
    environment:
    - HOST=db
    - USER=odoo
    - PASSWORD=和下面的要保持一致
  db:
    image: postgres:13 #如果是odoo 14建议用10
    restart: always
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_PASSWORD=和上面的要保持一致
      - POSTGRES_USER=odoo
    volumes:
      - ./data/db:/var/lib/postgresql/data
#docker-compose.yml文件结束
$ docker-compose up -d
#这样就会自动生成镜像,并启动容器

目前测试下来,这个方案非常方便,不管是安装插件还是修改源文件,只需要修改后重启容器就行。目前还没有研究出如何不重启容器,直接重启容器里的服务。以后迁移也非常方便,把整个目录打包,然后到新环境重新启动就行。所有的数据都还在。

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注