В настоящее время у меня возникают некоторые проблемы с докером и при подключении к нему проекта узла с Postgres.
Я уже задавал связанный вопрос, см. ссылка , однако я не могу обойти проблему соединения между 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
, как описано в этой ссылке ссылка
После перезапуска 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