【发布时间】:2019-11-15 05:26:45
【问题描述】:
我目前在使用 docker 并在其中运行时将节点项目与 Postgres 连接时遇到一些问题。
我已经问过一个相关问题,请参阅link,但是,我无法解决 nodeJs 和 postgres 之间的连接问题。
我的 docker-compose 文件如下所示:
# docker-compose.yml
version: "2.1"
services:
app:
build: .
ports:
- "49160:8080"
networks:
- webnet
depends_on:
db:
condition: service_healthy
environment:
DATABASE_URL: postgres://postgres:taskin@db:5432/mydb
db:
image: kartoza/postgis:9.6-2.4
networks:
- webnet
environment:
- POSTGRES_DB=mydb
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=mypassword
- POSTGRES_MULTIPLE_EXTENSIONS=postgis,pgrouting
- ALLOW_IP_RANGE=0.0.0.0/0
volumes:
- pgdata:/var/lib/postgresql/data
restart: on-failure
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
networks:
webnet:
volumes:
pgdata: {}
我的 Dockerfile 有以下输入:
#node 8
FROM node:8
#Create app directory
WORKDIR /usr/src/app
#Install app dependencies
COPY package*.json ./
RUN npm install
#Bundle app source
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
我的 nodeJS-express 服务器有以下设置:
var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://postgres:mypassword@db:5432/mydb";
var client = new pg.Client(conString);
client.connect()
当我运行 docker-compose up 时
我首先收到密码验证错误:我可以通过编辑
pg_hba.conf和postgresql.conf来解决此问题,如此链接中所述 link重新启动 PostgreSQL 并更改 postgres 用户的密码后,我将 SQL 备份文件恢复到容器内的 mydb 中
然后我停止了之前的 docker-compose 命令并再次执行了
sudo docker-compose up。获取以下输出
db_1_56896493481e |
db_1_56896493481e | postgres conf already configured
db_1_56896493481e | ssl already configured
db_1_56896493481e | pg_hba already configured
db_1_56896493481e | Setup master database
db_1_56896493481e | 2019-07-04 18:21:23.452 UTC [22] LOG: database system was interrupted; last known up at 2019-07-04 18:14:49 UTC
db_1_56896493481e | 2019-07-04 18:21:23.470 UTC [30] postgres@postgres FATAL: the database system is starting up
db_1_56896493481e | psql: FATAL: the database system is starting up
db_1_56896493481e | 2019-07-04 18:21:23.508 UTC [22] LOG: database system was not properly shut down; automatic recovery in progress
db_1_56896493481e | 2019-07-04 18:21:23.511 UTC [22] LOG: redo starts at 0/28ECC738
db_1_56896493481e | 2019-07-04 18:21:23.511 UTC [22] LOG: invalid record length at 0/28ECC770: wanted 24, got 0
db_1_56896493481e | 2019-07-04 18:21:23.511 UTC [22] LOG: redo done at 0/28ECC738
db_1_56896493481e | 2019-07-04 18:21:23.520 UTC [22] LOG: MultiXact member wraparound protections are now enabled
db_1_56896493481e | 2019-07-04 18:21:23.522 UTC [35] LOG: autovacuum launcher started
db_1_56896493481e | 2019-07-04 18:21:23.522 UTC [17] LOG: database system is ready to accept connections
db_1_56896493481e | List of databases
db_1_56896493481e | Name | Owner | Encoding | Collate | Ctype | Access privileges
db_1_56896493481e | ------------------+----------+----------+---------+---------+-----------------------
db_1_56896493481e | postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
db_1_56896493481e | mydb | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
db_1_56896493481e | template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
db_1_56896493481e | | | | | | postgres=CTc/postgres
db_1_56896493481e | template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
db_1_56896493481e | | | | | | postgres=CTc/postgres
db_1_56896493481e | template_postgis | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
db_1_56896493481e | (5 rows)
db_1_56896493481e |
db_1_56896493481e | postgres ready
db_1_56896493481e | Postgis Already There
db_1_56896493481e | HSTORE is only useful when you create the postgis database.
db_1_56896493481e | TOPOLOGY is only useful when you create the postgis database.
db_1_56896493481e | Setup postgres User:Password
db_1_56896493481e | ALTER ROLE
db_1_56896493481e | Check default db exists
db_1_56896493481e | mydb db already exists
db_1_56896493481e | List of databases
db_1_56896493481e | Name | Owner | Encoding | Collate | Ctype | Access privileges
db_1_56896493481e | ------------------+----------+----------+---------+---------+-----------------------
db_1_56896493481e | postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
db_1_56896493481e | mydb | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
db_1_56896493481e | template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
db_1_56896493481e | | | | | | postgres=CTc/postgres
db_1_56896493481e | template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
db_1_56896493481e | | | | | | postgres=CTc/postgres
db_1_56896493481e | template_postgis | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
db_1_56896493481e | (5 rows)
db_1_56896493481e |
db_1_56896493481e | 2019-07-04 18:21:24.742 UTC [17] LOG: received smart shutdown request
db_1_56896493481e | 2019-07-04 18:21:24.742 UTC [35] LOG: autovacuum launcher shutting down
db_1_56896493481e | 2019-07-04 18:21:24.743 UTC [32] LOG: shutting down
db_1_56896493481e | 2019-07-04 18:21:24.847 UTC [17] LOG: database system is shut down
db_1_56896493481e |
db_1_56896493481e | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1_56896493481e |
db_1_56896493481e | Postgres initialisation process completed .... restarting in foreground
db_1_56896493481e | 2019-07-04 18:21:25.886 UTC [129] LOG: database system was shut down at 2019-07-04 18:21:24 UTC
db_1_56896493481e | 2019-07-04 18:21:25.888 UTC [129] LOG: MultiXact member wraparound protections are now enabled
db_1_56896493481e | 2019-07-04 18:21:25.891 UTC [133] LOG: autovacuum launcher started
db_1_56896493481e | 2019-07-04 18:21:25.891 UTC [126] LOG: database system is ready to accept connections
app_1_a393ffc30f68 |
app_1_a393ffc30f68 | > node-api-postgres@1.0.0 start /usr/src/app
app_1_a393ffc30f68 | > node index.js
app_1_a393ffc30f68 |
app_1_a393ffc30f68 | Running on http://0.0.0.0:8080
【问题讨论】:
-
app_1_ca885d4626de | wait-for-it.sh: waiting 15 seconds for db:5432: 也许再等一下,因为第一个错误发生在db_1_ceb54072343c | 2019-07-04 12:23:14.921 UTC [18] LOG: database system is ready to accept connections之前大约 1 秒? -
现在我收到另一个错误消息:
Error: connect ECONNREFUSED 172.20.0.2:5432 -
另外,根据this,您可以使用
healthcheck(从未使用过),主要是depends_on。当您运行它时,最好将此wait-for-it.sh逻辑移动到作曲家。 -
你等了多久?我想说,要验证错误确实来自数据库未准备好,您应该等待 120 秒(当 Node 启动时,您的日志不应再显示与
db相关的内容,这样您就确定了问题是否来自两个容器之间缺乏同步)。 -
我删除了 tha wait-for-it.sh 命令,并在我的 docker compose 文件中将其替换为 depends_on: db,但仍然得到
Error: getaddrinfo ENOTFOUND db db:5432
标签: node.js postgresql docker docker-compose postgis