gpt4 book ai didi

python - 在 testing.postgresql 中,在 Docker 中找不到 initdb 命令

转载 作者:行者123 更新时间:2023-11-29 13:39:08 25 4
gpt4 key购买 nike

有一个similar thread有同样的问题,但无法为我工作。基本上,我正在尝试使用 testing.postgresqlmyproject 进行单元测试,并在 docker 容器中运行它。

但是,当我运行我的命令时,我得到一个RuntimeError: command not found: initdb

docker-compose exec myproject python3 -m unittest

这在 WSL 中有效,但在 docker 中无效。

看起来它无法找到 initdb。它与在 docker-compose.yml 中创建的数据库有冲突问题还是我在 docker 文件中遗漏了一些行?

我从 docs 中概述了我的测试示例:

class TestPostgresqlInteraction(unittest.TestCase):

Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)

with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
dbConn = psycopg2.connect(**postgresql.dsn())
cursor = dbConn.cursor()
def setUp(self):
self.postgresql = self.Postgresql()

def tearDown(self):
self.postgresql.stop()

def testMethod(self)
conn = psycopg2.connect(**self.postgresql.dsn())
cursor = conn.cursor()
# execute cursor and do tests...

def tearDownModule(self):
self.Postgresql.clear_cache()

docker-compose.yml

version: "3"
services:
myproject:
build:
context: .
dockerfile: ./myproject/Dockerfile
depends_on:
- database
volumes:
- ./myproject:/code
stdin_open: true
tty: true
database:
build:
context: .
dockerfile: ./database/Dockerfile
environment:
POSTGRES_USER_FILE: /secrets/dbUser.txt
POSTGRES_PASSWORD_FILE: /secrets/dbPassword.txt
ports:
- "8765:5432"
volumes:
- otherdata:/var/lib/postgresql/data

我的项目/Dockerfile

FROM python:3.7.3-stretch

RUN apt-get update && apt-get install -y \
poppler-utils

ARG moduleDir=myproject

WORKDIR /code

COPY secrets/ /secrets

# COPY $moduleDir/myproject/ ./

COPY $moduleDir/requirements.txt requirements.txt

RUN pip install -r requirements.txt

数据库/Dockerfile

FROM postgres:11.3

WORKDIR /secrets

COPY secrets/dbUser.txt dbUser.txt
COPY secrets/dbPassword.txt dbPassword.txt

RUN mkdir -p /docker-entrypoint-initdb.d

COPY database/schema.sql /docker-entrypoint-initdb.d

回溯:

ImportError: Failed to import test module: tests.testMyproject
File "/usr/local/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/code/tests/testmyProject.py", line 40, in <module>
class TestPostgresqlInteraction(unittest.TestCase):
File "/code/tests/testmyProject.py", line 46, in TestPostgresqlInteraction
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)
File "/usr/local/lib/python3.7/site-packages/testing/common/database.py", line 52, in __init__
self.cache = self.target_class(**settings_noautostart)
File "/usr/local/lib/python3.7/site-packages/testing/common/database.py", line 92, in __init__
self.initialize()
File "/usr/local/lib/python3.7/site-packages/testing/postgresql.py", line 50, in initialize
self.initdb = find_program('initdb', ['bin'])
File "/usr/local/lib/python3.7/site-packages/testing/postgresql.py", line 144, in find_program
raise RuntimeError("command not found: %s" % name)
RuntimeError: command not found: initdb

最佳答案

您的 Docker 容器可能缺少 which shell 命令。

testing.postgresql 调用 testing.common.database.get_path_of,然后执行 which 命令定位 initdb 程序:

def get_path_of(name):
if os.name == 'nt':
which = 'where'
else:
which = 'which'
try:
path = subprocess.Popen([which, name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0]
if path:
return path.rstrip().decode('utf-8')
else:
return None
except Exception:
return None

如果缺少 which,您可以使用包管理器添加它。这适用于我基于 CentOS 的容器:

yum 安装哪个

关于python - 在 testing.postgresql 中,在 Docker 中找不到 initdb 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58070698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com