使用sslmode运行postgres连接字符串(对于SQLAlchemy)的Docker错误

时间:2019-04-04 12:47:03

标签: python-3.x postgresql docker flask-sqlalchemy

我有一个可连接到AWS RDS的python应用程序。

连接字符串使我可以在Docker中正常运行应用程序:

postgresql://<usr>:<pwd>@<svr>/<db>

以下2个连接字符串与以下错误一起运行:

postgresql://<usr>:<pwd>@<svr>/<db>?sslmode=verify-full&sslrootcert=xxx

postgresql://<usr>:<pwd>@<svr>/<db>?sslmode=require

错误(底部代码段):

 Traceback (most recent call last):
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
     return self.wsgi_app(environ, start_response)
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
     response = self.handle_exception(e)
   File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
     return original_handler(e)
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
     reraise(exc_type, exc_value, tb)
   File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 34, in reraise
     raise value.with_traceback(tb)
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
     response = self.full_dispatch_request()
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
     rv = self.handle_user_exception(e)
   File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
     return original_handler(e)
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
     reraise(exc_type, exc_value, tb)
   File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 34, in reraise
     raise value.with_traceback(tb)
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
     rv = self.dispatch_request()
   File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)
   File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
     resp = resource(*args, **kwargs)
   File "/usr/local/lib/python3.7/site-packages/flask/views.py", line 88, in view
     return self.dispatch_request(*args, **kwargs)
   File "/usr/local/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
     resp = meth(*args, **kwargs)
   File "./database/database.py", line 29, in post
     database.save()
   File "./database/database.py", line 35, in save
     db.session.commit()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
     return getattr(self.registry(), name)(*args, **kwargs)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1026, in commit
     self.transaction.commit()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 493, in commit
     self._prepare_impl()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 472, in _prepare_impl
     self.session.flush()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2451, in flush
     self._flush(objects)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2589, in _flush
     transaction.rollback(_capture_exception=True)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
     compat.reraise(exc_type, exc_value, exc_tb)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
     raise value
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2549, in _flush
     flush_context.execute()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
     rec.execute(self)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
     uow,
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 213, in save_obj
     ) in _organize_states_for_save(base_mapper, states, uowtransaction):
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 374, in _organize_states_for_save
     base_mapper, uowtransaction, states
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/persistence.py", line 1585, in _connections_for_states
     connection = uowtransaction.transaction.connection(base_mapper)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 312, in connection
     return self._connection_for_bind(bind, execution_options)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 418, in _connection_for_bind
     conn = self._parent._connection_for_bind(bind, execution_options)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 430, in _connection_for_bind
     conn = bind._contextual_connect()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2226, in _contextual_connect
     self._wrap_pool_connect(self.pool.connect, None),
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2266, in _wrap_pool_connect
     e, dialect, self
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
     util.raise_from_cause(sqlalchemy_exception, exc_info)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 383, in raise_from_cause
     reraise(type(exception), exception, tb=exc_tb, cause=cause)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 128, in reraise
     raise value.with_traceback(tb)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
     return fn()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 354, in connect
     return _ConnectionFairy._checkout(self)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 751, in _checkout
     fairy = _ConnectionRecord.checkout(pool)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 483, in checkout
     rec = pool._do_get()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 138, in _do_get
     self._dec_overflow()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
     compat.reraise(exc_type, exc_value, exc_tb)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
     raise value
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 135, in _do_get
     return self._create_connection()
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 299, in _create_connection
     return _ConnectionRecord(self)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 428, in __init__
     self.__connect(first_connect_check=True)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 630, in __connect
     connection = pool._invoke_creator(self)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
     return dialect.connect(*cargs, **cparams)
   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
     return self.dbapi.connect(*cargs, **cparams)
   File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
 sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not create SSL context: library has no ciphers

当我使用所有3种连接类型运行时,它可以在除Docker之外的所有机器上运行。为什么只有在Docker中会发生这种情况?

我忘了声明,我正在使用SQLAlchemy连接到postgresql。

谢谢。

我的Dockerfile:

FROM python:3.7.2-stretch

COPY . /

RUN pip install -r requirements.txt

EXPOSE 5000

WORKDIR /models

CMD ["uwsgi", "--ini", "uwsgi.ini"]

我的docker-compose

version: '3.1'

services:

  modelling:
    build:
      context: ./modelling
    image: users-api
    container_name: modelling
    ports:
        - 5000:5000
    restart: always

我的要求:

aniso8601==6.0.0
argon2-cffi==19.1.0
atomicwrites==1.3.0
attrs==19.1.0
cffi==1.12.2
Click==7.0
Flask==1.0.2
Flask-JWT-Extended==3.18.0
Flask-RESTful==0.3.7
Flask-SQLAlchemy==2.3.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.1
more-itertools==7.0.0
pluggy==0.9.0
psycopg2-binary==2.7.7
py==1.8.0
pycparser==2.19
PyJWT==1.7.1
pytest==4.3.1
pytz==2018.9
six==1.12.0
SQLAlchemy==1.3.1
uWSGI==2.0.18
Werkzeug==0.15.1

1 个答案:

答案 0 :(得分:1)

尝试在您的requirements.txt中更改以下字符串:

psycopg2-binary==2.7.7

psycopg2==2.7.7 --no-binary :all: