![](/img/trans.png)
[英]testing.postgresql command not found: initdb inside docker
[英]In testing.postgresql, cannot find initdb command inside Docker
有一個類似的線程有同樣的問題,但無法為我工作。 基本上,我正在嘗試使用testing.postgresql
對myproject
進行單元測試,並且我在 docker 容器中運行它。
但是,當我運行我的命令時,我得到一個RuntimeError: command not found: initdb
,
docker-compose exec myproject python3 -m unittest
這適用於 WSL,但不適用於 docker。
看起來它無法找到 initdb。 是否與在 docker-compose.yml 中創建的數據庫存在沖突問題,或者我是否缺少 docker 文件中的某些行?
我概述了我的測試,例如文檔中的示例:
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
使用testing.postgresql
意味着您將獲得由該工具准備的新 PG 實例 - 正如他們的文檔中所述。 您需要做的是指向其他 docker。 只需使用psycopg
並連接到database
:
dsn = "dbname='" + RDS_DBNAME + "' user='" + RDS_USERNAME + "' host='database' password='" + RDS_PASSWORD + "'"
dbConn = psycopg2.connect(dsn)
除此之外 - 你看起來with
奇怪。 但這只能是我的印象。
您的 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
您可以使用 package 管理器添加它。 這適用於我基於 CentOS 的容器:
yum install which
要使用testing.postgresql
您需要訪問我的項目 Docker 中的myproject
服務器,而不僅僅是database
ZC5FD2727.CDD0D2B3C24 本地臨時測試數據庫將連接到另一個數據庫以獲取數據,但這仍然意味着您需要在 myproject 中的 postgresql 服務器的本地實例。
在您的myproject/Dockerfile
,只需更改第一個RUN
並在poppler-utils
之后添加postgresql
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.