2019年11月4日 星期一
安裝docker
windows系統,參考 docker官方文檔
Mac系統,參考 docker官方文檔
構建自定義ODOO鏡像
標准ODOO鏡像可能不包含特別的python模塊,或者Linux工具,此時需要 自定義 Odoo鏡像
寫dockerfile
編寫dockerfile,例如加入需要的python庫
➜ 10.1 git:(master) ✗ cat Dockerfile
FROM odoo:10.0
MAINTAINER Odoo S.A. <info@odoo.com>
USER root
COPY ./pip.conf /root/.pip/pip.conf
RUN set -x; \
pip install pypinyin pypdf
# Set default user when running the container
USER odoo
ENTRYPOINT ["/entrypoint.sh"]
CMD ["odoo"]
說明: 上面的自定義鏡像,是在Odoo10 基礎上,安裝了 pypinyin 模塊,為了使用本地pip鏡像,例如 pip.conf 內容
➜ 10.1 git:(master) ✗ cat pip.conf
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
構建鏡像
基於dockerfile構建鏡像
➜ 10.1 git:(master) ✗ docker build . -t odoo:10.1
Sending build context to Docker daemon 3.072kB
Step 1/8 : FROM odoo:10.0
---> 50bfb7575fe2
Step 2/8 : MAINTAINER Odoo S.A. <info@odoo.com>
---> Using cache
---> 353b1366ee28
Step 3/8 : USER root
---> Using cache
---> 27ec1ca1072c
Step 4/8 : COPY ./pip.conf /root/.pip/pip.conf
---> Using cache
---> ebdd6547d4e1
Step 5/8 : RUN set -x; pip install pypinyin pypdf
---> Using cache
---> 72edd5d9d792
Step 6/8 : USER odoo
---> Using cache
---> 0cc904972ec2
Step 7/8 : ENTRYPOINT ["/entrypoint.sh"]
---> Using cache
---> e4738346b7a3
Step 8/8 : CMD ["odoo"]
---> Using cache
---> 793edee6ab30
Successfully built 793edee6ab30
Successfully tagged odoo:10.1
這樣,就會建立odoo:10.1 鏡像
比如 docker images查看鏡像
➜ 10.1 git:(master) ✗ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
odoo 10.1 793edee6ab30 3 days ago 894MB <<<<<
<none> <none> 2ebbc09b340c 4 days ago 888MB
<none> <none> 5e1be85e3ee9 4 days ago 894MB
<none> <none> cd0e2acac50b 4 days ago 536MB
<none> <none> 317e442f4416 4 days ago 561MB
<none> <none> 7d6ae7c50fb6 4 days ago 549MB
<none> <none> 73c08dfaaf64 4 days ago 546MB
pycharm_helpers PY-183.6156.16 0430ed2d37ee 6 days ago 37.1MB
odoo 13.0 b77d7d215af3 7 days ago 1.14GB
<none> <none> 7b449bc0b8bd 7 days ago 535MB
odoo 11.0 ac8c1f2da96a 11 days ago 1.07GB
odoo 12.0 a914ad271b31 11 days ago 1.15GB
<none> <none> 687217ff7424 2 weeks ago 84.1MB
postgres 12 f88dfa384cc4 2 weeks ago 348MB
odoo 10.0 50bfb7575fe2 2 weeks ago 888MB
debian stretch-slim c2f145c34384 2 weeks ago 55.3MB
debian buster-slim 105ec214185d 2 weeks ago 69.2MB
debian latest 8e9f8546050d 2 weeks ago 114MB
busybox latest 19485c79a9bb 2 months ago 1.22MB
shadowsocks/shadowsocks-libev latest 4ae4e89442e8 2 months ago 17.4MB
dpage/pgadmin4 latest 15aebd95450f 3 months ago 237MB
postgres 10 897b33033d64 3 months ago 230MB
postgres 11 53912975086f 3 months ago 312MB
mplatform/mquery latest 0e11d82ddb1d 2 years ago 7.11MB
使用docker compose編排 Odoo
odoo是基於多個服務,用docker compose 對這些服務進行編排,會比較方便。
編寫 docker-compose.yml
編寫 docker-compose.yml 文件,內容如下
➜ odoo10c cat docker-compose.yml
version: '3.3'
services:
# Web Application Service Definition
# --------
#
# All of the information needed to start up an odoo web
# application container.
web:
image: odoo:10.1
depends_on:
- db
- pgadmin
# Port Mapping
# --------
#
# Here we are mapping a port on the host machine (on the left)
# to a port inside of the container (on the right.) The default
# port on Odoo is 8069, so Odoo is running on that port inside
# of the container. But we are going to access it locally on
# our machine from localhost:9000.
ports:
- 9000:8069
# Data Volumes
# --------
#
# This defines files that we are mapping from the host machine
# into the container.
#
# Right now, we are using it to map a configuration file into
# the container and any extra odoo modules.
volumes:
- ./config:/etc/odoo
# - ./addons:/mnt/extra-addons
- ../../git-repo/geely-mts:/mnt/extra-addons
# Odoo Environment Variables
# --------
#
# The odoo image uses a few different environment
# variables when running to connect to the postgres
# database.
#
# Make sure that they are the same as the database user
# defined in the db container environment variables.
environment:
- HOST=db
- USER=odoo
- PASSWORD=odoo
# Database Container Service Definition
# --------
#
# All of the information needed to start up a postgresql
# container.
db:
image: postgres:11
# Database Environment Variables
# --------
#
# The postgresql image uses a few different environment
# variables when running to create the database. Set the
# username and password of the database user here.
#
# Make sure that they are the same as the database user
# defined in the web container environment variables.
environment:
- POSTGRES_PASSWORD=odoo
- POSTGRES_USER=odoo
- POSTGRES_DB=postgres # Leave this set to postgres
pgadmin:
image: dpage/pgadmin4
depends_on:
- db
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
volumes:
pgadmin:
說明:
1. 在 Odoo 服務,使用自定義的鏡像,例如 Odoo:10.1
2. 編排了 PG服務
3. 編排 PGADMIN 方便對PG 進行管理
測試 docker-compose.yml
使用 docker-compose 啟動 Odoo, 運行命令
➜ odoo10c docker-compose up
odoo10c_db_1 is up-to-date
odoo10c_pgadmin_1 is up-to-date
Recreating odoo10c_web_1 ... done
Attaching to odoo10c_db_1, odoo10c_pgadmin_1, odoo10c_web_1
pgadmin_1 | NOTE: Configuring authentication for SERVER mode.
pgadmin_1 |
pgadmin_1 | [2019-11-03 13:02:57 +0000] [1] [INFO] Starting gunicorn 19.9.0
pgadmin_1 | [2019-11-03 13:02:57 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
第一次運行 docker-compose 時,會創建相關的容器,上面的例子顯示更新容器,是因為容器之前已經創建好。
此時,用瀏覽器訪問 http://127.0.0.1:9000 即可訪問到 Odoo服務 ; 訪問 http://127.0.0.1:5050 即可訪問到 pgadmin
Odoo鏡像說明
官方 Odoo鏡像會在 在 docker-compose.yml 所在目錄建立 2個目錄,用於掛載到 Odoo容器用做 volume,其中:addons掛載到 /mnt/extra-addons , 以及 config 掛載到 /etc/odoo
Odoo容器默認使用 /etc/odoo/odoo.conf 作為配置文件。
所以,1,如果要自定義配置,修改 config/odoo.conf 文件即可,可以從 odoo docker 項目拷貝 原始 配置文件 作為 config/odoo.conf ;2,如果要掛載自定義的ADDONS,掛載到 addons 目錄即可。
Pycharm 調用 docker compose 遠程運行Odoo
配置pycharm 使用 docker compose
使用 pycharm 將 Odoo 模塊項目導入
在 preference ,選擇 項目解釋器
在項目解釋器, 點擊 ⚙️圖標,選擇 ADD
在彈窗,選擇 docker compose
在 server 處,選擇 docker 服務器,或者 新建docker 服務器。
注意:
如果是 windows平台,需要 關閉Docker TLS ,如何關閉,具體 參考PYcharm官方文檔,或者docker 文檔
在 configuration file 選擇前面建立的服務編排 docker-compose.yml 文件
在 service 選擇 web。 注意, Pycharm 自動識別出 編排文件包含的所有服務,並且按字母排序
然后,點擊OK 確認。
pycharm將會 去docker容器,偵測python的版本,完成后,遠程解釋器將會配置如下圖所示
使用 遠程解釋器運行 Odoo
建立 開發專用 Odoo配置
因為 pycharm 會將 項目 掛載到 容器的 /opt/project 下,如Odoo默認的 extra-addons不同;
所以,需要為 開發建立一個專用的 配置文件,例如 config/odoo-dev.conf 。
注意,這個文件存放在 docker-compose.yml文件目錄
在這個 文件里面將 addons_path 指向 /opt/project
例如
addons_path = /opt/project
備注:
項目被掛載到 /opt/project 時由pycharm 生成的 docker compose 所指定
配置 run configuration
在 run 菜單,選擇 edit configuration,在彈窗
在 腳本 輸入 /usr/bin/odoo
在 參數 輸入 -c /etc/odoo/odoo-dev.conf
在 python 解釋器,選擇 前面建立的 遠程python
運行 Odoo
點擊 run 按鈕,運行 Odoo
pycharm將 調用 docker compose 運行Odoo,如圖