Q: Error in communication between containers with flask and postgres I have a problem when trying to make the connection between containers using flask, nginx, and postgres. The following error appears:
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?

Flask connection:
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/

Does anyone have any suggestions?

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

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

localhost to database and port to 5432 then docker-compose up again

A: Thank you! It worked using:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@database:5432/plataforma_testes'

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

