使用 docker-compose,我正在運行一個 postgres 容器(db)。資料本身永久存盤在我的 Windows 機器上。這有效。我無法讓另一個容器運行 python 應用程式來訪問資料庫。
我的 docker-compose 檔案如下,其中我使用 ## 表示我嘗試過的一些選項:
version: '0.1'
services
db: #also the server name
image: postgres:15.0
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: my_password
##POSTGRES_HOST_AUTH_METHOD: trust
##POSTGRES_HOST: postgres
##PGDATA: d:/pg #/var/lib/postgresql/data/pgdata
ports:
#port on machine:port on container
- 1234:5432
volumes:
#path on my windows machine:path to postgres default folder
- D:/pg:/var/lib/postgresql/data
privileged:
true
app:
image: simple_python_debug
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=my_password
- POSTGRES_PORT=5432
- POSTGRES_DB_NAME=postgres
- POSTGRES_HOSTNAME=localhost
depends_on:
- db
我的 python 腳本的 dockerfile (simple_python_debug) 是
FROM python:3.9
# Install pip requirements
COPY requirements.txt .
RUN pip3 install -r requirements.txt
WORKDIR /app #this is where the .py file is located
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
CMD ["python", "test.py"]
postgres 的 dockerfile 很簡單:
services:
db:
image: postgres:15.0
restart: always
environment:
POSTGRES_PASSWORD: my_password
test.py(存盤在 /app 中)包含
import os
from sqlalchemy import create_engine
port = os.getenv('POSTGRES_PORT')#'5432'
password = os.getenv('POSTGRES_PASSWORD')#'my_password'
user = os.getenv('POSTGRES_USER')#'postgres'
hostname = os.getenv('POSTGRES_HOSTNAME') #'localhost'
database_name = os.getenv('POSTGRES_DB_NAME') #'postgres'
connection_params = 'postgresql psycopg2://' user ':' password '@' hostname '/' database_name
# \ "connect_args = {'options': '-csearch_path={}'.format(" schema ')}'
engine = create_engine(connection_params)
在我的本地 Windows 機器上運行 docker 桌面并在本地 Windows 機器上關閉 postgres 服務以進行良好的衡量,我運行
.../test>docker-compose up從PowerShell 7;正在作業的 postgres 容器的部分訊息是:
test-db-1 |
test-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
test-db-1 |
test-db-1 | 2022-11-12 15:27:14.665 UTC [1] LOG: starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110 1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
test-db-1 | 2022-11-12 15:27:14.665 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
test-db-1 | 2022-11-12 15:27:14.665 UTC [1] LOG: listening on IPv6 address "::", port 5432
test-db-1 | 2022-11-12 15:27:14.674 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
test.py 呼叫 create_engine 失敗;錯誤訊息的尾部是
test-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
test-app-1 | TCP/IP connections on port 5432?
test-app-1 | could not connect to server: Cannot assign requested address
test-app-1 | Is the server running on host "localhost" (::1) and accepting
test-app-1 | TCP/IP connections on port 5432?
test-app-1 |
test-app-1 | (Background on this error at: https://sqlalche.me/e/14/e3q8)
有幾個堆疊溢位相關的問題和錯誤訊息。甚至還有關于在 python 和 docker 中使用 postgres 的中等文章。
https://stefanopassador.medium.com/docker-compose-with-python-and-posgresql-45c4c5174299
Docker - 如何在 postgres 容器中運行 psql 命令?
無法將 python 應用程式容器連接到本地 postgres
從 Docker 容器內部,如何連接到機器的本地主機?
也許我遺漏了一些簡單的東西,但是任何建議或指向適當的 docker 檔案都可以幫助我擺脫困境。
編輯:正如@JRichardz 所推薦的,完整的錯誤訊息如下:
test-app-1 | Traceback (most recent call last):
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect
test-app-1 | return fn()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 307, in connect
test-app-1 | return _ConnectionFairy._checkout(self)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
test-app-1 | fairy = _ConnectionRecord.checkout(pool)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
test-app-1 | rec = pool._do_get()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
test-app-1 | self._dec_overflow()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
test-app-1 | compat.raise_(
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
test-app-1 | raise exception
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
test-app-1 | return self._create_connection()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
test-app-1 | return _ConnectionRecord(self)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
test-app-1 | self.__connect()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
test-app-1 | pool.logger.debug("Error on connect(): %s", e)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
test-app-1 | compat.raise_(
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
test-app-1 | raise exception
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
test-app-1 | connection = pool._invoke_creator(self)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect
test-app-1 | return dialect.connect(*cargs, **cparams)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect
test-app-1 | return self.dbapi.connect(*cargs, **cparams)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
test-app-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
test-app-1 | psycopg2.OperationalError: could not connect to server: Connection refused
test-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
test-app-1 | TCP/IP connections on port 5432?
test-app-1 | could not connect to server: Cannot assign requested address
test-app-1 | Is the server running on host "localhost" (::1) and accepting
test-app-1 | TCP/IP connections on port 5432?
test-app-1 |
test-app-1 |
test-app-1 | The above exception was the direct cause of the following exception:
test-app-1 |
test-app-1 | Traceback (most recent call last):
test-app-1 | File "/app/btc_sma_recommendations_1_inflection.py", line 51, in <module>
test-app-1 | with engine.connect() as connection:
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3166, in connect
test-app-1 | return self._connection_cls(self, close_with_result=close_with_result)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
test-app-1 | else engine.raw_connection()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3245, in raw_connection
test-app-1 | return self._wrap_pool_connect(self.pool.connect, _connection)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3215, in _wrap_pool_connect
test-app-1 | Connection._handle_dbapi_exception_noconnection(
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2069, in _handle_dbapi_exception_noconnection
test-app-1 | util.raise_(
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
test-app-1 | raise exception
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect
test-app-1 | return fn()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 307, in connect
test-app-1 | return _ConnectionFairy._checkout(self)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
test-app-1 | fairy = _ConnectionRecord.checkout(pool)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
test-app-1 | rec = pool._do_get()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
test-app-1 | self._dec_overflow()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
test-app-1 | compat.raise_(
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
test-app-1 | raise exception
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
test-app-1 | return self._create_connection()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
test-app-1 | return _ConnectionRecord(self)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
test-app-1 | self.__connect()
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
test-app-1 | pool.logger.debug("Error on connect(): %s", e)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
test-app-1 | compat.raise_(
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
test-app-1 | raise exception
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
test-app-1 | connection = pool._invoke_creator(self)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 578, in connect
test-app-1 | return dialect.connect(*cargs, **cparams)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 584, in connect
test-app-1 | return self.dbapi.connect(*cargs, **cparams)
test-app-1 | File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
test-app-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
test-app-1 | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
test-app-1 | Is the server running on host "localhost" (127.0.0.1) and accepting
test-app-1 | TCP/IP connections on port 5432?
test-app-1 | could not connect to server: Cannot assign requested address
test-app-1 | Is the server running on host "localhost" (::1) and accepting
test-app-1 | TCP/IP connections on port 5432?
test-app-1 |
test-app-1 | (Background on this error at: https://sqlalche.me/e/14/e3q8)
test-app-1 exited with code 1
test-db-1 |
test-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
test-db-1 |
test-db-1 | 2022-11-14 00:00:02.724 UTC [1] LOG: starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110 1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
test-db-1 | 2022-11-14 00:00:02.724 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
test-db-1 | 2022-11-14 00:00:02.724 UTC [1] LOG: listening on IPv6 address "::", port 5432
test-db-1 | 2022-11-14 00:00:02.731 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
test-db-1 | 2022-11-14 00:00:02.771 UTC [29] LOG: database system was shut down at 2022-11-13 02:41:42 UTC
test-db-1 | 2022-11-14 00:00:02.867 UTC [1] LOG: database system is ready to accept connections
uj5u.com熱心網友回復:
該日志是關鍵:
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
在 docker 中沒有localhost。
為了證明這一點,只需將 localhost 替換為運行 postgress 的 ip。
要了解為什么 localhost 應該在 docker 和 docker-compose 中使用以及解決方案是什么,請檢查:
- https://stackoverflow.com/a/52213178/3957754
- https://stackoverflow.com/a/63207679/3957754
- https://stackoverflow.com/a/71879028/3957754
- https://stackoverflow.com/a/54617222/3957754
如果這對您有用,請在問題中添加確切的錯誤日志以幫助后代
uj5u.com熱心網友回復:
在使用它們之前定義卷會更好。
此外,如果您希望 postgres 的資料持續存在,您需要將檔案夾放在卷內
/var/lib/postgresql/資料
就像是
volumes:
- fancy-volume:
... fancy definition..
services:
fancy-app:
...
volumes:
- fancy-volume:/var/lib/postgresql/data
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533192.html
標籤:PythonPostgreSQL码头工人码头工人撰写港口
上一篇:“進行安裝”錯誤。make(e=2)系統找不到指定的檔案
下一篇:復制子目錄和嵌套子目錄的所有檔案
