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