Docker Compose + TypeORM: No Connection

Viewed 35

I know there are plenty of questions around this topic, but I can't make it work for my (fairly straightforward setup). What am I missing? I guess we can neglect pgadmin out of the equation, but I kept in there for full disclosure.

docker-compose.yml

version: '3.8'

services:
  postgres:
    image: postgres:14.2-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: mydatabase
      PGDATA: /data/mydatabase
    volumes:
      - pgstore:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    image: dpage/pgadmin4:6.5
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-admin@admin.com}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
    volumes:
      - pgadmin:/root/.pgadmin
    ports:
      - '5050:80'
    networks:
      - postgres
    depends_on:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  pgstore:
  pgadmin:

Starting it with docker-compose up yields:

Creating network "my-app_postgres" with driver "bridge"
Creating my-app_postgres_1 ... done
Creating my-app_pgadmin_1  ... done
Attaching to my-app_postgres_1, my-app_pgadmin_1
postgres_1  | The files belonging to this database system will be owned by user "postgres".
postgres_1  | This user must also own the server process.
postgres_1  |
postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
postgres_1  | The default database encoding has accordingly been set to "UTF8".
postgres_1  | The default text search configuration will be set to "english".
postgres_1  |
postgres_1  | Data page checksums are disabled.
postgres_1  |
postgres_1  | fixing permissions on existing directory /data/mydatabase ... ok
postgres_1  | creating subdirectories ... ok
postgres_1  | selecting dynamic shared memory implementation ... posix
postgres_1  | selecting default max_connections ... 100
postgres_1  | selecting default shared_buffers ... 128MB
postgres_1  | selecting default time zone ... UTC
postgres_1  | creating configuration files ... ok
postgres_1  | running bootstrap script ... ok
postgres_1  | performing post-bootstrap initialization ... sh: locale: not found
postgres_1  | 2022-09-06 14:03:48.965 UTC [32] WARNING:  no usable system locales were found
postgres_1  | ok
postgres_1  | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
postgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1  | --auth-local and --auth-host, the next time you run initdb.
postgres_1  | ok
postgres_1  |
postgres_1  |
postgres_1  | Success. You can now start the database server using:
postgres_1  |
postgres_1  |     pg_ctl -D /data/mydatabase -l logfile start
postgres_1  |
postgres_1  | waiting for server to start....2022-09-06 14:03:49.890 UTC [38] LOG:  starting PostgreSQL 14.2 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres_1  | 2022-09-06 14:03:49.891 UTC [38] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2022-09-06 14:03:49.894 UTC [39] LOG:  database system was shut down at 2022-09-06 14:03:49 UTC
postgres_1  | 2022-09-06 14:03:49.896 UTC [38] LOG:  database system is ready to accept connections
postgres_1  |  done
postgres_1  | server started
postgres_1  | CREATE DATABASE
postgres_1  |
postgres_1  |
postgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1  |
postgres_1  | waiting for server to shut down....2022-09-06 14:03:50.195 UTC [38] LOG:  received fast shutdown request
postgres_1  | 2022-09-06 14:03:50.196 UTC [38] LOG:  aborting any active transactions
postgres_1  | 2022-09-06 14:03:50.197 UTC [38] LOG:  background worker "logical replication launcher" (PID 45) exited with exit code 1
postgres_1  | 2022-09-06 14:03:50.197 UTC [40] LOG:  shutting down
postgres_1  | 2022-09-06 14:03:50.204 UTC [38] LOG:  database system is shut down
postgres_1  |  done
postgres_1  | server stopped
postgres_1  |
postgres_1  | PostgreSQL init process complete; ready for start up.
postgres_1  |
postgres_1  | 2022-09-06 14:03:50.307 UTC [1] LOG:  starting PostgreSQL 14.2 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
postgres_1  | 2022-09-06 14:03:50.308 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2022-09-06 14:03:50.308 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2022-09-06 14:03:50.310 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2022-09-06 14:03:50.313 UTC [52] LOG:  database system was shut down at 2022-09-06 14:03:50 UTC
postgres_1  | 2022-09-06 14:03:50.315 UTC [1] LOG:  database system is ready to accept connections
pgadmin_1   | NOTE: Configuring authentication for SERVER mode.
pgadmin_1   |
pgadmin_1   | [2022-09-06 14:03:56 +0000] [1] [INFO] Starting gunicorn 20.1.0
pgadmin_1   | [2022-09-06 14:03:56 +0000] [1] [INFO] Listening at: http://[::]:80 (1)
pgadmin_1   | [2022-09-06 14:03:56 +0000] [1] [INFO] Using worker: gthread
pgadmin_1   | [2022-09-06 14:03:56 +0000] [89] [INFO] Booting worker with pid: 89

In my application, I initialize TypeORM with the following options:

const options = {
  default: {
    type: process.env.DATABASE_TYPE,
    host: process.env.DATABASE_HOST,
    port: process.env.DATABASE_PORT,
    username: process.env.DATABASE_USERNAME,
    password: process.env.DATABASE_PASSWORD,
    database: process.env.DATABASE_NAME,
    synchronize: true,
    logging: false,
  },
};

Whereas my .env holds the following information:

DATABASE_TYPE=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=mydatabase

I can access the container on the command line with:

docker exec -it my-app psql -U postgres mydatabase

and list the \l the databases and see mydatabase. But I get Did not find any relations. for \d because the application does not connect to the database and therefore there are no relations yet.

More information:

  • the application is running outside of the container
  • the application is not throwing an error, it just tries to connect indefinitely
  • I am using Docker container on MacOS

What am I missing here? Any help appreciated!

0 Answers
Related