I'm trying to run a dockrized express API for the first time, and from all the troubles i had, this is the one that im not able to find an answer here.
When i run the command docker-compose up --build
I get to see the node running Server is running on port 3000
and the server running database system is ready to accept connections
But when i try do make a call to any API endpoint, i get the following error:
error: no PostgreSQL user name specified in startup packet
Can someone help ?
Here are some files im dealing with:
docker-compose.yml
version: "3"
services:
app:
build: .
depends_on:
- postgres
ports:
- "3000:3000"
- "9229:9229"
postgres:
image: postgres:11.2-alpine
volumes:
- ./db:/docker-entrypoint-initdb.d/
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
- APP_DB_USER=docker
- APP_DB_PASS=docker
- APP_DB_NAME=docker
Dockerfile
FROM node
EXPOSE 3000 9229
WORKDIR /home/app
COPY package.json /home/app
COPY package-lock.json /home/app
RUN npm ci
COPY . /home/app
RUN npm run build
CMD ["npm", "run", "dev"]
docker-entrypoint-initdb.d script
#!/bin/bash
set -e
export PGPASSWORD=$POSTGRES_PASSWORD;
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER $APP_DB_USER WITH PASSWORD '$APP_DB_PASS';
CREATE DATABASE $APP_DB_NAME;
GRANT ALL PRIVILEGES ON DATABASE $APP_DB_NAME TO $APP_DB_USER;
\connect $APP_DB_NAME $APP_DB_USER
BEGIN;
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
COMMIT;
EOSQL
The connection is created this way
const pool = new Pool({
database: process.env.APP_DB_NAME,
user: process.env.APP_DB_USER,
host: "postgres",
password: process.env.APP_DB_PASS,
});