現在、dockerで問題が発生しており、ノードプロジェクトをPostgres内で実行しているときにPostgresに接続しています。
リンクを参照して関連する質問をすでに行っていますが、nodeJと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-composeupを実行すると
私が最初にパスワード認証エラーが表示されます:私は編集してこの問題を解決することができpg_hba.conf
そしてpostgresql.conf
、このリンクに記載されたリンク
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 | > [email protected] 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 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)
}
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加