flask无法将PostgreSQL与docker-compose连接

王旺|

我使用docker-compose用postgresql构建了一个flask应用程序。但是flask无法连接postgresql。错误信息:

     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
    Is the server running on host "postgres-db" (192.168.16.2) and accepting
    TCP/IP connections on port 5432?

当我进入docker容器并ping时postgres-db,它运行良好。我启动了flask应用程序,它也运行良好。仅当我使用docker-compose up时才无法连接

我的docker-compose文件:

version: '3.7'

services:
  postgres-db:
    restart: always
    image: postgres:11.1-alpine   
    privileged: true  
    ports:
      - 5432:5432  
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: Aa123456 
      PGDATA: /var/lib/postgresql/data/pgdata 
    volumes:
      - ./db/postgres/data:/var/lib/postgresql/data/pgdata

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    volumes:
      - './api:/usr/src/app'
    ports:
      - 5002:5000
    environment:
      - FLASK_CONFIG=development
      - FLASK_ENV=development
      - APP_SETTINGS=project.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:Aa123456@postgres-db:5432/cms
      - DATABASE_TEST_URL=postgres://postgres:Aa123456@postgres-db:5432/cms_test
      - SECRET_KEY=ZQbn05PDeA7v11
    depends_on:
      - postgres-db
    container_name: api
    links: 
      - postgres-db:postgres-db

  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    restart: unless-stopped
    ports:
      - 8080:8080
    depends_on:
      - api
      - client

  client:
    build:
      context: ./client
      dockerfile: Dockerfile
    volumes:
      - './client:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - 3008:3000
    environment:
      - NODE_ENV=development
      - REACT_APP_SERVICE_URL=http://localhost:8080
      - CHOKIDAR_USEPOLLING=true
    depends_on:
      - api
幼虫

您正在遇到争用条件:应用程序尝试在数据库可用之前连接到数据库。这不是Docker特有的问题;如果您的应用程序依赖于外部资源(例如数据库服务器),那么您需要编写代码来处理这种情况。

一个简单的解决方案是阻止应用程序启动,直到数据库可用为止。SELECT 1循环运行并等待成功就足够了。一个执行此操作的示例shell脚本如下所示:

while ! psql -h postgresb-db -c 'select 1'; do
  sleep 1
done

如果在flask应用程序中使用sqlachemy,则可以执行类似的操作:

from sqlalchemy import create_engine
from sqlalchemy import exc
import time


while 1:
    try:
        e = create_engine('postgres://lars@localhost/sandbox')
        e.execute('select 1')
    except exc.OperationalError:
        print('Waiting for database...')
        time.sleep(1)
    else:
        break

print('Connected!')

上面的方法在解决启动争用条件方面效果很好,但是不能处理在应用程序运行时数据库服务器重新启动的情况。在这种情况下,您需要妥善处理断开连接/重新连接。就代码的设计而言,这需要做更多的工作,并且超出了此答案的范围。有关此主题的一些讨论,请参见此答案

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章