我使用docker-compose,我尝试从Web容器连接到postgres数据库。 我使用这个URI:
postgresql://hola:hola@postgres/holadb
我收到此错误:
Connection refused
Is the server running on host "postgres" (172.18.0.2) and accepting
TCP/IP connections on port 5432?
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
我在构建和运行之前删除./data/postgres。
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
我不明白为什么它不起作用。提前感谢您的帮助。
答案 0 :(得分:4)
您需要设置网络以允许容器之间的通信。这样的事情必须奏效:
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: {}}
答案 1 :(得分:2)
当postgres仍在初始化时,Web容器尝试连接... 等待一些延迟解决了我的问题。
编辑:我使用Docker Compose Healthcheck来执行此操作。