带烧瓶和Postgres的容器之间的通信错误

时间:2020-10-29 10:14:36

标签: docker nginx flask

尝试使用flask,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'

docker-compose:

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/

有人有什么建议吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

谢谢!它可以使用:

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
相关问题