简体   繁体   English

如何通过 docker-compose 网络连接到 postgres?

[英]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.我使用 docker-compose 并尝试从 Web 容器连接到 postgres 数据库。 I use this URI:我使用这个 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 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.我在构建和运行之前删除 ./data/postgres 。

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.预先感谢您的帮助。

The web container tries to connect while postgres is still initializing... Waiting some delay solved my issue.当 postgres 仍在初始化时,Web 容器尝试连接......等待一些延迟解决了我的问题。

EDIT: I use Docker Compose Healthcheck to do this.编辑:我使用Docker Compose Healthcheck来做到这一点。

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 .更多信息在这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM