gpt4 book ai didi

node.js - 无法从应用程序容器连接到 postgres DB docker 容器

转载 作者:行者123 更新时间:2023-11-29 13:40:07 25 4
gpt4 key购买 nike

我目前在使用 docker 时遇到一些问题,并且在运行时将 Node 项目与 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 时

  1. 我首先收到密码验证错误:我可以通过编辑 pg_hba.confpostgresql.conf 来解决此问题,如此链接 link 中所述

  2. 重启 PostgreSQL 并更改 postgres 用户的密码后,我将 SQL 备份文件恢复到容器内的 mydb 中

  3. 然后我停止了之前的 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




最佳答案

经过多次尝试,我可以修复它。我的 docker-compose 文件是正确的,我的 ajax 请求 没有正确定义。改变后

 $.ajax({
url: 'http://db:8080/show',
type: "POST",
data: CoordjsonObject,
dataType: "json",
success: function(data) {
addStreetsLayer(data);
console.log("worked!")
},
error: function(xhr) {
console.log(xhr)
}
});

 $.ajax({
url: 'http://IP-ADRESS-OF-NODE-CONTAINER:8080/show',
type: "POST",
data: CoordjsonObject,
dataType: "json",
success: function(data) {
addStreetsLayer(data);
console.log("worked!")
},
error: function(xhr) {
console.log(xhr)
}
});

关于node.js - 无法从应用程序容器连接到 postgres DB docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56888470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com