我目前在docker上遇到一些问题,并且在其中运行Postgres时将其与Nodegres连接。
我已经问过一个相关问题,请参见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
答案 0 :(得分:0)
尝试了很多之后,我可以修复它。我的docker-compose文件正确,我的ajax request
定义不正确。更改后
$.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)
}
});