2

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,
});
  • `docker-entrypoint-initdb.d` is a folder, not a script. Also, you should create an sql script, not a bash script : https://stackoverflow.com/questions/26598738/how-to-create-user-database-in-script-for-docker-postgres – Aserre Dec 06 '21 at 00:22
  • I got this way of doing this way from: [here](https://graspingtech.com/docker-compose-postgresql/) I tried to follow whats in the thread you posted but got several diferente erros, like npm didnt exist and such :(. Sorry but i didnt understood your "furthermore", could you explain ? – Cesar Bhering Dec 06 '21 at 00:50
  • I didn't quite catch what you were doing, sorry. Are you able to connect to your container, launch postgres and run `\dt` ? If so, do you see your table ? You can also check at the same time if your `APP_DB_*` variables are correctly initialized – Aserre Dec 06 '21 at 02:35
  • 1
    Ah, I see what is probably going wrong : you have 2 containers running, one for the app, one for the db. You can have a look at this : https://stackoverflow.com/questions/36283908/re-using-environment-variables-in-docker-compose-yml – Aserre Dec 06 '21 at 02:42
  • That was it man! Thanks! God bless you! – Cesar Bhering Dec 06 '21 at 12:41

0 Answers0