I'm building a project with Node, Knex, PostgreSQL and I'm Dockerizing my project. The main issue I have I'm unable to run the migration and also to get the data from the DB using Knex. What I can do is use DB GUI such as Tableplus to connect to the DB.
You can find my project here: Repo of the project
All I read online is not working for me as
- use the service name \ container name -> not working :(
- use full postgres url as
postgresql://root:password@postgres/dbname'-> not working :( - IP address as 0.0.0.0 not working
- IP address as 127.0.0.1 I can use only to run migrations/seeds but not to use the DB as GET/POST
- I can use docker.internal.host but then cannot do the migration then
I'm getting always this error
getaddrinfo ENOTFOUND pokedex-db-postgres
I have no more ideas what is wrong so I'm sharing my configuration hoping someone will help me to understand
Docker file
# Builder
FROM node:17.3
RUN mkdir -p /home/node/code/node_modules && chown -R node:node /home/node/code
WORKDIR /home/node/code
COPY package*.json ./
USER node
RUN yarn install
COPY --chown=node:node . .
EXPOSE 8800
ENTRYPOINT [ "yarn", "run", "dev" ]
Docker compose
version: '3.9'
services:
node:
build: .
container_name: pokedex-backend-node
env_file:
- .env
depends_on:
postgresDB:
condition: service_started
ports:
- '8800:8800'
restart: always
volumes:
- ./:/home/node/code
postgresDB:
container_name: pokedex-db-postgres
env_file:
- .env
image: postgres:14.1
networks:
backend-database: null
postgres-pgadmin: null
expose:
- '5343:5432'
ports:
- '5343:5432'
restart: always
volumes:
- ./volumes/postgres-data:/var/lib/postgresql/data
- ./volumes/postgres-init:/docker-entrypoint-initdb.d
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U root']
interval: 10s
start_period: 10s
timeout: 4s
retries: 3
db-admin-pgadmin:
container_name: pokedex-dbadmin-pgadmin
depends_on:
postgresDB:
condition: service_started
env_file:
- .env
image: dpage/pgadmin4:latest
networks:
postgres-pgadmin: null
ports:
- '8900:80'
volumes:
- ./volumes/pgadmin-data:/var/lib/pgadmin
networks:
backend-database: {}
postgres-pgadmin: {}
Knex configuration
const development = {
client: 'pg',
connection: {
host: 'pokedex-db-postgres',
port: config.POSTGRES_PORT,
database: config.POSTGRES_DB,
user: config.POSTGRES_USER,
password: config.POSTGRES_PASSWORD,
},
pool: {
min: 5,
max: 50,
createTimeoutMillis: 3000,
acquireTimeoutMillis: 30000,
idleTimeoutMillis: 30000,
reapIntervalMillis: 1000,
createRetryIntervalMillis: 100,
afterCreate: function (conn, done) {
conn.query('SELECT 1;', function (err) {
if (err) {
log.error('Connection to DB failed %s', err);
}
done(err, conn);
});
},
},
debug: dev ? true : false,
migrations: {
directory: './src/database/migrations',
},
seeds: {
directory: './src/database/seeds',
},
useNullAsDefault: true,
};