2

I'm running into the following issue when trying to run an app in docker with docker-compose. Judging from other posts here on SO, my docker-compose.yml file looks correct (which seems to be causing the issue in the majority of cases).

So not sure why the db does not exist in this case?

20] FATAL:  database "db" does not exist

Dockerfile

FROM node:12.18.2-stretch-slim

# Set a directory for the app
WORKDIR /usr/src/app

# Install dependencies
RUN apt-get update && apt-get -y install procps
RUN yarn global add typeorm

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN yarn install

# Copy all source files to the container
COPY . .

EXPOSE 3000

docker-compose.yml

version: '3'
services:
  redis:
    image: redis
    ports:
      - '6380:6379'
    volumes:
      - redis:/data
  db:
    image: postgres:11
    user: postgres
    environment:
      POSTGRES_USER: '${ORM_USERNAME}'
      POSTGRES_PASSWORD: '${ORM_PASSWORD}'
      POSTGRES_MULTIPLE_DATABASES: '${ORM_DATABASE}, ${ORM_TEST_DATABASE}'
    ports:
      - '5434:5432'
    volumes:
      - ./pg-init-scripts/:/docker-entrypoint-initdb.d/
      - pg-data:/var/lib/postgresql/data
  api:
    build: .
    command: yarn start:dev
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - '3000:3000'
    depends_on:
      - db
      - redis
volumes:
  pg-data:
    driver_opts:
      type: none
      device: '${HOME}/db-pg-data'
      o: bind
  redis:


#!/bin/bash

set -e
set -u

function create_user_and_database() {
    local database=$1
    echo "  Creating user and database '$database'"
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
        CREATE USER $database;
        CREATE DATABASE $database;
        GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}

if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
    echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
    for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
        create_user_and_database $db
    done
    echo "Multiple databases created"
fi
octavemirbeau
  • 481
  • 6
  • 19
  • What does the code look like that connects to your database? How are you passing database credentials into the api container? Where did you find documentation that suggested the `POSTGRES_MULTIPLE_DATABASES` variable was meaningful to the `postgres` image? – larsks Feb 10 '22 at 11:58
  • This is the docker setup used at my current work, and it works for everyone else in the team besides me. I'm the only one in an OSX environment though. Env variables are passed via a .env file. POSTGRES_MULTIPLE_DATABASES: https://github.com/mrts/docker-postgresql-multiple-databases – octavemirbeau Feb 10 '22 at 15:16
  • You're not using an image built from the repository to which you just linked. The official `postgres` image doesn't support `POSTGRES_MULTIPLE_DATABASES` so it's not creating any of your databases. – larsks Feb 10 '22 at 15:38
  • Then the question is why it works for the rest of the team, as we use the same docker-compose.yml file, which means we all pull the same img. – octavemirbeau Feb 10 '22 at 16:06
  • I just added the .sh script used to create the db:s which does the same as: github.com/mrts/docker-postgresql-multiple-databases So that's why it works, and why it should work for me too. – octavemirbeau Feb 10 '22 at 16:17
  • With that script in place, it all just works for me: https://asciinema.org/a/AWNB1FzafSBIB2wwPofDapHZV. Are you setting those `ORM_*` environment variables in a `.env` file? – larsks Feb 10 '22 at 17:42
  • Exactly all the env variables are set in a .env file, I can also see that linux is able to copy with right permissions. I was thinking that perhaps the bash script was not being executed. – octavemirbeau Feb 10 '22 at 18:23
  • This seems to be related to the latest OSX. Another team member has the exact same issue now when trying to run it on 'Monterey'. A team member who's still on 'Big Sur' doesn't have any issues and can run the container. – octavemirbeau Feb 10 '22 at 22:07
  • 1
    This was the solution: https://stackoverflow.com/a/60881015/11826960 – octavemirbeau Feb 10 '22 at 22:32
  • I used the same exact script and encountered the same exact error. The script originates from an article that details how to run multiple databases from the same Docker container URL: https://github.com/mrts/docker-postgresql-multiple-databases/blob/master/create-multiple-postgresql-databases.sh – Quan Truong Oct 17 '22 at 20:20

0 Answers0