1

我正在尝试在带有 odoo 的 docker 容器中使用 shell,但我遇到了一些问题。

我通过 docker-compose 设置本地环境,更准确地说,我在一个网络中运行 odoo 和 PostgreSQL,但是当我想为 odoo shell 指定 db 时,它不起作用。

这是我的 docker-compose.yaml:

version: '3'
services:
  db:
    image: postgres:10
    ports:
      - '5432:5432'
    environment:
    - POSTGRES_USER=odoo
    - POSTGRES_PASSWORD=odoo
    - POSTGRES_DB=postgres
    volumes:
    - odoo-db-data:/var/lib/postgresql/data
  odoo:
    depends_on:
    - db
    build: .
    ports:
    - '8069:8069'
    - '8071:8071'
    - '4444:4444'
    volumes:
    - odoo-web-data:/var/lib/odoo
    - ./local_addons/:/mnt/extra-addons/
    - ./odoo.conf:/etc/odoo/odoo.conf
    stdin_open: true
    tty: true

volumes:
  odoo-web-data:
  odoo-db-data:

和 Dockerfile(这很简单):

FROM odoo:13

USER root
RUN pip3 install remote-pdb pdbpp dateutils xlrd
USER odoo

odoo 实例本身工作得很好,因此 odoo 必须能够连接到数据库。但是当我尝试像这样执行shell时:

docker exec -it odoo_odoo_1 bash -c "odoo shell -d postgres"

我有这个例外(为了便于阅读,跟踪未满):

2019-10-29 14:37:13,094 72 INFO ? odoo: Odoo version 13.0-20191022 
2019-10-29 14:37:13,095 72 INFO ? odoo: Using configuration file at /etc/odoo/odoo.conf 
2019-10-29 14:37:13,095 72 INFO ? odoo: addons paths: ['/usr/lib/python3/dist-packages/odoo/addons', '/var/lib/odoo/addons/13.0', '/mnt/extra-addons'] 
2019-10-29 14:37:13,095 72 INFO ? odoo: database: default@default:default 
2019-10-29 14:37:13,272 72 INFO ? odoo.addons.base.models.ir_actions_report: Will use the Wkhtmltopdf binary at /usr/local/bin/wkhtmltopdf 
2019-10-29 14:37:13,377 72 INFO ? odoo.service.server: Initiating shutdown 
2019-10-29 14:37:13,377 72 INFO ? odoo.service.server: Hit CTRL-C again or send a second signal to force the shutdown. 
2019-10-29 14:37:13,378 72 INFO ? odoo.sql_db: Connection to the database failed 
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 60, in __new__
    return cls.registries[db_name]
  File "/usr/lib/python3/dist-packages/odoo/tools/func.py", line 69, in wrapper
    return func(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/odoo/tools/lru.py", line 44, in __getitem__
    a = self.d[obj].me
KeyError: 'postgres'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/odoo", line 8, in <module>
    odoo.cli.main()
  File "/usr/lib/python3/dist-packages/odoo/cli/command.py", line 60, in main
    o.run(args)
.....
  File "/usr/lib/python3/dist-packages/odoo/sql_db.py", line 600, in borrow
    **connection_info)
  File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

我没有明确指定 postgres 主机和端口(这是我的 odoo.conf):

[options]

addons_path = /mnt/extra-addons
data_dir = /var/lib/odoo
debug_mode = True

; db_host = db
; db_port = 5432
; db_user = odoo
; db_password = odoo
; db_name = postgres

你有什么想法为什么我无法连接到数据库?

4

3 回答 3

2

docker-compose 创建多个 docker 容器,一个运行 Odoo,另一个运行 Posgresql,依此类推,基于 yaml 配置,实际上它们是具有不同 IP 地址的不同机器。所以启动命令:

  docker exec -it odoo_odoo_1 bash -c "odoo shell -d postgres"

odoo 将尝试连接到引用 localhost 的 postgres,在这种情况下,它是运行 odoo 的容器,而不是 postgres。如果您指定参数 --db_host 和 --db_password 应该可以工作:

  docker-compose exec odoo odoo shell -d <DB-NAME-CREATED-WITH-ODOO> --db_host db --db_password odoo

或者

  docker exec -it odoo_odoo_1 bash -c "odoo shell -d <DB-NAME-CREATED-WITH-ODOO> --db_host db --db_password odoo"

例子

在https://hub.docker.com/_/odoo/#中创建 docker compose !:

docker-compose.yaml:

  version: '2'
  services:
    web:
      image: odoo:12.0
      depends_on:
        - mydb
      ports:
        - "8069:8069"
      environment:
      - HOST=mydb
      - USER=odoo
      - PASSWORD=myodoo
    mydb:
      image: postgres:10
      environment:
        - POSTGRES_DB=postgres
        - POSTGRES_PASSWORD=myodoo
        - POSTGRES_USER=odoo

运行 docker-compose:

  docker-compose up -d

连接到 localhost:8069 并创建名为“my-test-db”的新数据库,然后使用以下命令登录到 shell:

  docker-compose exec web odoo shell -d my-test-db --db_host mydb --db_password myodoo```
于 2020-04-24T13:25:48.687 回答
0

Odooshell需要以与您使用 docker 容器启动时相同的配置运行,/etc/odoo/odoo.conf. 基本上使用此命令odoo shell -d postgres,您正在运行 Odoo shell 实例而无需任何配置,并且 Odoo 应用程序数据库选择为postgres,将其更改为以下

docker exec -it odoo_odoo_1 bash -c "odoo shell -c /etc/odoo/odoo.conf -d ODOO_DATABASE_NAME"

如果您打开调试模式,Odoo 数据库名称将显示在括号内右上角的用户名部分之后。

数据库名称

于 2019-10-29T15:14:52.297 回答
0

编写自定义 Dockerfile 解决了这个问题(由于其他一些原因,我编写了自己的 Dockerfile,但它确实解决了我的 odoo shell 问题)。如果有人遇到我的问题,这是我的本地环境:

Dockerfile:

FROM debian:buster-slim

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

# Install some deps, lessc and less-plugin-clean-css, and wkhtmltopdf
RUN set -x; \
        apt-get update \
        && apt-get install -y --no-install-recommends \
            ca-certificates \
            curl \
            dirmngr \
            fonts-noto-cjk \
            gnupg \
            libssl-dev \
            node-less \
            npm \
            unzip \
            gcc \
            python3-dev \
            python3-psycopg2 \
            python3-num2words \
            python3-pip \
            python3-phonenumbers \
            python3-pyldap \
            python3-qrcode \
            python3-renderpm \
            python3-setuptools \
            python3-vobject \
            python3-watchdog \
            python3-xlwt \
            xz-utils \
        && curl -o wkhtmltox.deb -sSL https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb \
        && echo '7e35a63f9db14f93ec7feeb0fce76b30c08f2057 wkhtmltox.deb' | sha1sum -c - \
        && apt-get install -y --no-install-recommends ./wkhtmltox.deb \
        && rm -rf /var/lib/apt/lists/* wkhtmltox.deb

# install latest postgresql-client
RUN set -x; \
        echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' > etc/apt/sources.list.d/pgdg.list \
        && export GNUPGHOME="$(mktemp -d)" \
        && 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 -y postgresql-client \
        && rm -rf /var/lib/apt/lists/*

# Install rtlcss (on Debian buster)
RUN set -x; \
    npm install -g rtlcss

# Download odoo source from github
RUN adduser --disabled-password --gecos "" odoo \
        && mkdir -p /odoo \
        && chown -R odoo /odoo \
        && curl -LOk https://github.com/OCA/OCB/archive/13.0.zip \
        && unzip 13.0.zip \
        && mv ./OCB-13.0/* /odoo/ \
        && rm -rf ./OCB-13.0 \
        && rm -rf 13.0.zip

COPY ./requirements.txt /
RUN pip3 install -r /requirements.txt

ENV ODOO_RC /etc/odoo/odoo.conf
RUN mkdir -p /var/lib/odoo \
        && mkdir -p /mnt/extra-addons \
        && mkdir -p /etc/odoo \
        && touch /etc/odoo/odoo.conf \
        && chown -R odoo /var/lib/odoo \
        && chown -R odoo /mnt/extra-addons \
        && chown -R odoo /etc/odoo/odoo.conf

USER odoo

CMD ["/odoo/odoo-bin"]

码头工人-compose.yaml:

version: '3'
services:
  db:
    image: postgres:10
    ports:
      - '5432:5432'
    environment:
    - POSTGRES_USER=odoo
    - POSTGRES_PASSWORD=odoo
    - POSTGRES_DB=postgres
    volumes:
    - odoo-db-data:/var/lib/postgresql/data
  odoo:
    depends_on:
    - db
    build: .
    ports:
    - '8069:8069'
    - '8071:8071'
    - '4444:4444'
    volumes:
    - odoo-web-data:/var/lib/odoo
    - ./local_addons/:/mnt/extra-addons/
    - ./odoo.conf:/etc/odoo/odoo.conf
    stdin_open: true
    tty: true

volumes:
  odoo-web-data:
  odoo-db-data:

odoo.conf:

[options]

addons_path = /mnt/extra-addons,/odoo/addons
data_dir = /var/lib/odoo
debug_mode = True

db_host = db
db_port = 5432
db_user = odoo
db_password = odoo
; db_name = postgres

要求.txt:

Babel==2.3.4
chardet==3.0.4
decorator==4.0.10
docutils==0.12
ebaysdk==2.1.5
feedparser==5.2.1
gevent==1.1.2 ; sys_platform != 'win32' and python_version < '3.7'
gevent==1.3.4 ; sys_platform != 'win32' and python_version >= '3.7'
gevent==1.4.0 ; sys_platform == 'win32' and python_version >= '3.7'
greenlet==0.4.10 ; python_version < '3.7'
greenlet==0.4.13 ; python_version >= '3.7'
html2text==2016.9.19
Jinja2==2.10.1
libsass==0.12.3
lxml==3.7.1 ; sys_platform != 'win32' and python_version < '3.7'
lxml==4.2.3 ; sys_platform != 'win32' and python_version >= '3.7'
lxml ; sys_platform == 'win32'
Mako==1.0.4
MarkupSafe==0.23
mock==2.0.0
num2words==0.5.6
ofxparse==0.16
passlib==1.6.5
Pillow==5.4.1
Pillow==6.1.0 ; sys_platform == 'win32' and python_version >= '3.7'
polib==1.1.0
psutil==4.3.1; sys_platform != 'win32'
psutil==5.6.3; sys_platform == 'win32'
psycopg2==2.7.3.1; sys_platform != 'win32'
psycopg2==2.8.3; sys_platform == 'win32'
pydot==1.2.3
pyldap==2.4.28; sys_platform != 'win32'
pyparsing==2.1.10
PyPDF2==1.26.0
pyserial==3.1.1
python-dateutil==2.5.3
pytz==2016.7
pyusb==1.0.0
qrcode==5.3
reportlab==3.3.0
requests==2.20.0
zeep==3.1.0
vatnumber==1.2
vobject==0.9.3
Werkzeug==0.14.1
XlsxWriter==0.9.3
xlwt==1.3.*
xlrd==1.0.0
pypiwin32 ; sys_platform == 'win32'

这个环境主要基于标准的 odoo 图像,但我从 github repo 的源代码构建它。

我正在像这样运行 shell,没有指定 odoo 配置文件:

docker exec -it odoo_odoo_1 bash -c "/odoo/odoo-bin shell -d ODOO_DATABASE_NAME"
于 2019-10-30T17:07:50.217 回答