Q:

How to connect to postgres through docker-compose network?

I use docker-compose and I try to connect to the postgres database from the web container.
I use this URI:
postgresql://hola:hola@postgres/holadb

I get this error:
Connection refused
    Is the server running on host "postgres" (172.18.0.2) and accepting
    TCP/IP connections on port 5432?

docker-compose.yml
version: '2'

services:
    web:
        restart: always
        build: ./web
        expose:
            - "8000"
        volumes:
            - /usr/src/app/project/static
        command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
        depends_on:
            - postgres

    postgres:
        image: postgres:9.6
        ports:
            - "5432:5432"
        environment:
            - POSTGRES_USER=hola
            - POSTGRES_PASSWORD=hola
            - POSTGRES_DB=holadb
        volumes:
            - ./data/postgres:/var/lib/postgresql/data

I remove ./data/postgres before building and running.
Logs
postgres_1  | The files belonging to this database system will be owned by user "postgres".
postgres_1  | This user must also own the server process.
postgres_1  |
postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
postgres_1  | The default database encoding has accordingly been set to "UTF8".
postgres_1  | The default text search configuration will be set to "english".
postgres_1  |
postgres_1  | Data page checksums are disabled.
postgres_1  |
postgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1  | creating subdirectories ... ok
postgres_1  | selecting default max_connections ... 100
postgres_1  | selecting default shared_buffers ... 128MB
postgres_1  | selecting dynamic shared memory implementation ... posix
postgres_1  | creating configuration files ... ok
postgres_1  | running bootstrap script ... ok
web_1       | [2017-06-03 16:54:14 +0000] [1] [INFO] Starting gunicorn 19.7.1
web_1       | [2017-06-03 16:54:14 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_1       | [2017-06-03 16:54:14 +0000] [1] [INFO] Using worker: sync
web_1       | [2017-06-03 16:54:14 +0000] [7] [INFO] Booting worker with pid: 7
web_1       | [2017-06-03 16:54:14 +0000] [8] [INFO] Booting worker with pid: 8
postgres_1  | performing post-bootstrap initialization ... ok
postgres_1  |
postgres_1  | WARNING: enabling "trust" authentication for local connections
postgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1  | --auth-local and --auth-host, the next time you run initdb.
postgres_1  | syncing data to disk ... ok
postgres_1  |
postgres_1  | Success. You can now start the database server using:
postgres_1  |
postgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1  |
postgres_1  | waiting for server to start....LOG:  database system was shut down at 2017-06-03 16:54:16 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started
postgres_1  |  done
postgres_1  | server started
postgres_1  | CREATE DATABASE
postgres_1  |
postgres_1  | CREATE ROLE
postgres_1  |
postgres_1  |
postgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1  |
postgres_1  | LOG:  received fast shutdown request
postgres_1  | LOG:  aborting any active transactions
postgres_1  | LOG:  autovacuum launcher shutting down
postgres_1  | LOG:  shutting down
postgres_1  | waiting for server to shut down....LOG:  database system is shut down
postgres_1  |  done
postgres_1  | server stopped
postgres_1  |
postgres_1  | PostgreSQL init process complete; ready for start up.
postgres_1  |
postgres_1  | LOG:  database system was shut down at 2017-06-03 16:54:18 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started

I don't understand why it does not work. Thank you in advance for your help.

A:

You need to setup a network to allow communication between containers. Something like this should work:
version: '2'
services:
    web:
        restart: always
        build: ./web
        expose:
            - "8000"
        volumes:
            - /usr/src/app/project/static
        command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
        depends_on:
            - postgres
        networks: ['mynetwork']

    postgres:
        restart: always
        build:
            context: ./postgresql
        volumes_from:
            - data
        ports:
            - "5432:5432"
        networks: ['mynetwork']
networks: {mynetwork: {}}

More info here and here.

A:

The web container tries to connect while postgres is still initializing...
Waiting some delay solved my issue.
EDIT: I use Docker Compose Healthcheck to do this.

