【问题标题】:Error in communication between containers with flask and postgres带有烧瓶和postgres的容器之间的通信错误
【发布时间】:2020-10-29 21:57:49
【问题描述】:

我在尝试使用烧瓶、nginx 和 postgres 建立容器之间的连接时遇到问题。出现如下错误:

flask       | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
flask       |   Is the server running on host "localhost" (127.0.0.1) and accepting
flask       |   TCP/IP connections on port 5454?
flask       | could not connect to server: Cannot assign requested address
flask       |   Is the server running on host "localhost" (::1) and accepting
flask       |   TCP/IP connections on port 5454?

烧瓶连接:

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@localhost:5454/plataforma_testes'

码头工人撰写:

version: "3.3"

services:
    flask:
        build: ./flask
        container_name: flask
        restart: always
        environment: 
            - APP_NAME=PlataformDeTestes
            - DB_USERNAME=postgres
        expose:
            - 8080
        links:
            - database
        depends_on:
            - database

    nginx:
        build: ./nginx
        container_name: nginx
        restart: always
        ports:
            - "80:80"

    database:
        image: postgres:10
        env_file: postgres/.env
        ports:
          - "5454:5432"
        volumes:
          - /docker/volumes/postgres:/var/lib/postgresql/

有人有什么建议吗?

【问题讨论】:

    标签: docker nginx flask


    【解决方案1】:

    改变:

    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@localhost:5454/plataforma_testes'
    

    到:

    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@database:5432/plataforma_testes'
    

    localhostdatabase 和端口到 5432 然后 docker-compose up 再次

    【讨论】:

    • 我做了建议的更改,但问题仍然存在
    • 还需要是标准数据库端口5432; ports: 不被考虑(或必需)。
    • @DavidMaze 所说的有效,但现在他有这个错误:```flask | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user "postgres" ``` 但是用户和密码被定义为 "postgres" 和 "admin"
    【解决方案2】:

    谢谢!它使用:

    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@database:5432/plataforma_testes'
    

    database:
     image: postgres:10
     env_file: postgres/.env
     volumes:
        - /docker/volumes/postgres:/var/lib/postgresql/data
    

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多