Reputation: 228
A simple docker postgres with Postgresql connecting to Python/Django can't connect to the database. Why isn't this working?
Start script (here I am cleaning the initial set up as much as I can - all volumes removed, the data file for postgres purged, and everything restarted):
#!/bin/bash
rm -rf ./data
mkdir data
cd data
mkdir db
cd ..
docker-compose down
docker rm -f $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker-compose up
Docker File (as far as I can tell this is the correct configuration):
FROM python:3.8-alpine
RUN apk update && apk add --no-cache \
gcc python3-dev \
postgresql-libs postgresql-dev musl-dev
RUN mkdir /code
COPY requirements.txt /code
WORKDIR /code
RUN apk add --no-cache --upgrade bash
RUN apk add python3 py3-pip
RUN apk add libffi-dev
RUN pip3 install -r ./requirements.txt --verbose
COPY . /code/
I've tried a couple different ways of creating the Dockerfile. Tere are two different ways of making the healthcheck wait on postgres. Both aren't working, when actually either should and I don't know why.
METHOD 1
Here's the docker-compose based on (https://github.com/zigsphere/statping/blob/dev/docker-compose.yml) using a healthcheck on postgres:
version: "3.9"
services:
db:
restart: always
image: postgres
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_NAME=dev-postgres
- POSTGRES_USER=pixel
- POSTGRES_DATABASE=lightchan
- POSTGRES_PASSWORD=stardust
healthcheck:
test: ["CMD-SHELL", "pg_isready -U pixel"]
interval: 10s
timeout: 5s
retries: 5
ports:
- 5432:5432
web:
build: .
restart: always
command: sh -c "python3 manage.py runserver"
#command: sh -c "./waitfor.sh db:5432 -- python3 manage.py runserver"
volumes:
- .:/code
ports:
- "8001:8001"
environment:
- POSTGRES_NAME=dev-postgres
- POSTGRES_USER=pixel
- POSTGRES_DATABASE=lightchan
- POSTGRES_PASSWORD=stardust
depends_on:
db:
condition: service_healthy
The specific error:
lightchan-db-1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
lightchan-db-1 | psql:/docker-entrypoint-initdb.d/init.sql: error: could not read from input file: Is a directory
lightchan-db-1 exited with code 1
The verbose output:
./testing.sh
[+] Running 3/3
⠿ Container lightchan-web-1 Removed 0.1s
⠿ Container lightchan-db-1 Removed 0.1s
⠿ Network lightchan_default Removed 0.1s
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
"docker volume rm" requires at least 1 argument.
See 'docker volume rm --help'.
Usage: docker volume rm [OPTIONS] VOLUME [VOLUME...]
Remove one or more volumes
[+] Running 3/3
⠿ Network lightchan_default Created 0.0s
⠿ Container lightchan-db-1 Created 0.1s
⠿ Container lightchan-web-1 Created 0.1s
Attaching to lightchan-db-1, lightchan-web-1
lightchan-db-1 | The files belonging to this database system will be owned by user "postgres".
lightchan-db-1 | This user must also own the server process.
lightchan-db-1 |
lightchan-db-1 | The database cluster will be initialized with locale "en_US.utf8".
lightchan-db-1 | The default database encoding has accordingly been set to "UTF8".
lightchan-db-1 | The default text search configuration will be set to "english".
lightchan-db-1 |
lightchan-db-1 | Data page checksums are disabled.
lightchan-db-1 |
lightchan-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
lightchan-db-1 | creating subdirectories ... ok
lightchan-db-1 | selecting dynamic shared memory implementation ... posix
lightchan-db-1 | selecting default max_connections ... 100
lightchan-db-1 | selecting default shared_buffers ... 128MB
lightchan-db-1 | selecting default time zone ... Etc/UTC
lightchan-db-1 | creating configuration files ... ok
lightchan-db-1 | running bootstrap script ... ok
lightchan-db-1 | performing post-bootstrap initialization ... ok
lightchan-db-1 | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
lightchan-db-1 | You can change this by editing pg_hba.conf or using the option -A, or
lightchan-db-1 | --auth-local and --auth-host, the next time you run initdb.
lightchan-db-1 | ok
lightchan-db-1 |
lightchan-db-1 |
lightchan-db-1 | Success. You can now start the database server using:
lightchan-db-1 |
lightchan-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
lightchan-db-1 |
lightchan-db-1 | waiting for server to start....2022-03-02 14:09:35.947 UTC [61] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
lightchan-db-1 | 2022-03-02 14:09:35.949 UTC [61] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
lightchan-db-1 | 2022-03-02 14:09:35.968 UTC [62] LOG: database system was shut down at 2022-03-02 14:09:34 UTC
lightchan-db-1 | 2022-03-02 14:09:35.995 UTC [61] LOG: database system is ready to accept connections
lightchan-db-1 | done
lightchan-db-1 | server started
lightchan-db-1 | CREATE DATABASE
lightchan-db-1 |
lightchan-db-1 |
lightchan-db-1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
lightchan-db-1 | psql:/docker-entrypoint-initdb.d/init.sql: error: could not read from input file: Is a directory
lightchan-db-1 exited with code 1
lightchan-db-1 |
lightchan-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
lightchan-db-1 |
lightchan-db-1 | 2022-03-02 14:09:41.911 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
lightchan-db-1 | 2022-03-02 14:09:41.911 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
lightchan-db-1 | 2022-03-02 14:09:41.911 UTC [1] LOG: listening on IPv6 address "::", port 5432
lightchan-db-1 | 2022-03-02 14:09:41.917 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
lightchan-db-1 | 2022-03-02 14:09:41.941 UTC [28] LOG: database system was interrupted; last known up at 2022-03-02 14:09:38 UTC
lightchan-db-1 | 2022-03-02 14:09:44.256 UTC [28] LOG: database system was not properly shut down; automatic recovery in progress
lightchan-db-1 | 2022-03-02 14:09:44.276 UTC [28] LOG: redo starts at 0/16FB5C8
lightchan-db-1 | 2022-03-02 14:09:44.277 UTC [28] LOG: invalid record length at 0/16FB6C0: wanted 24, got 0
lightchan-db-1 | 2022-03-02 14:09:44.277 UTC [28] LOG: redo done at 0/16FB678 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
lightchan-db-1 | 2022-03-02 14:09:44.306 UTC [1] LOG: database system is ready to accept connections
lightchan-web-1 | Watching for file changes with StatReloader
lightchan-db-1 | 2022-03-02 14:09:50.095 UTC [43] FATAL: database "lightchan" does not exist
lightchan-web-1 | Exception in thread django-main-thread:
lightchan-web-1 | Traceback (most recent call last):
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 211, in connect
lightchan-web-1 | self.connection = self.get_new_connection(conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
lightchan-web-1 | connection = Database.connect(**conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
lightchan-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
lightchan-web-1 | psycopg2.OperationalError: connection to server at "db" (172.25.0.2), port 5432 failed: FATAL: database "lightchan" does not exist
lightchan-web-1 |
lightchan-web-1 |
lightchan-web-1 | The above exception was the direct cause of the following exception:
lightchan-web-1 |
lightchan-web-1 | Traceback (most recent call last):
lightchan-web-1 | File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
lightchan-web-1 | self.run()
lightchan-web-1 | File "/usr/local/lib/python3.8/threading.py", line 870, in run
lightchan-web-1 | self._target(*self._args, **self._kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
lightchan-web-1 | fn(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 127, in inner_run
lightchan-web-1 | self.check_migrations()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 505, in check_migrations
lightchan-web-1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
lightchan-web-1 | self.loader = MigrationLoader(self.connection)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
lightchan-web-1 | self.build_graph()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 223, in build_graph
lightchan-web-1 | self.applied_migrations = recorder.applied_migrations()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
lightchan-web-1 | if self.has_table():
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
lightchan-web-1 | with self.connection.cursor() as cursor:
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 270, in cursor
lightchan-web-1 | return self._cursor()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 246, in _cursor
lightchan-web-1 | self.ensure_connection()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
lightchan-web-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 211, in connect
lightchan-web-1 | self.connection = self.get_new_connection(conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
lightchan-web-1 | connection = Database.connect(**conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
lightchan-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
lightchan-web-1 | django.db.utils.OperationalError: connection to server at "db" (172.25.0.2), port 5432 failed: FATAL: database "lightchan" does not exist
lightchan-web-1 |
Why I don't understand the error:
psql:/docker-entrypoint-initdb.d/init.sql
is very specifically not a directory, but the file (obviously). The permissions on both the docker-entrypoint-initdb.d
folder and the init.sql
file are open and so Docker should be able to access them.
METHOD 2
The other way I've been testing Docker has been to use the waitfor.sh
package (https://github.com/Eficode/wait-for) as recommended here (https://docs.docker.com/compose/startup-order/), and which is alpine linux compatible. The package is a simple drop in bash file that pipes in the command arguments in a complicated looking while loop that just does all the health checking. I've used packages like it before and they've worked.
Here's the docker-compose:
version: "3.9"
services:
db:
restart: always
image: postgres
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_NAME=dev-postgres
- POSTGRES_USER=pixel
- POSTGRES_DATABASE=lightchan
- POSTGRES_PASSWORD=stardust
#healthcheck:
# test: ["CMD-SHELL", "pg_isready -U pixel"]
# interval: 10s
# timeout: 5s
# retries: 5
ports:
- 5432:5432
web:
build: .
restart: always
command: sh -c "python3 manage.py runserver"
#command: sh -c "./waitfor.sh db:5432 -- python3 manage.py runserver"
volumes:
- .:/code
ports:
- "8001:8001"
environment:
- POSTGRES_NAME=dev-postgres
- POSTGRES_USER=pixel
- POSTGRES_DATABASE=lightchan
- POSTGRES_PASSWORD=stardust
depends_on:
- db
#condition: service_healthy
The verbose output:
./testing.sh
[+] Running 3/3
⠿ Container lightchan-web-1 Removed 0.1s
⠿ Container lightchan-db-1 Removed 0.1s
⠿ Network lightchan_default Removed 0.0s
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
"docker volume rm" requires at least 1 argument.
See 'docker volume rm --help'.
Usage: docker volume rm [OPTIONS] VOLUME [VOLUME...]
Remove one or more volumes
[+] Running 3/3
⠿ Network lightchan_default Created 0.0s
⠿ Container lightchan-db-1 Created 0.1s
⠿ Container lightchan-web-1 Created 0.1s
Attaching to lightchan-db-1, lightchan-web-1
lightchan-db-1 | The files belonging to this database system will be owned by user "postgres".
lightchan-db-1 | This user must also own the server process.
lightchan-db-1 |
lightchan-db-1 | The database cluster will be initialized with locale "en_US.utf8".
lightchan-db-1 | The default database encoding has accordingly been set to "UTF8".
lightchan-db-1 | The default text search configuration will be set to "english".
lightchan-db-1 |
lightchan-db-1 | Data page checksums are disabled.
lightchan-db-1 |
lightchan-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
lightchan-db-1 | creating subdirectories ... ok
lightchan-db-1 | selecting dynamic shared memory implementation ... posix
lightchan-db-1 | selecting default max_connections ... 100
lightchan-db-1 | selecting default shared_buffers ... 128MB
lightchan-db-1 | selecting default time zone ... Etc/UTC
lightchan-db-1 | creating configuration files ... ok
lightchan-web-1 | Watching for file changes with StatReloader
lightchan-web-1 | Exception in thread django-main-thread:
lightchan-web-1 | Traceback (most recent call last):
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 211, in connect
lightchan-web-1 | self.connection = self.get_new_connection(conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
lightchan-web-1 | connection = Database.connect(**conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
lightchan-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
lightchan-web-1 | psycopg2.OperationalError: connection to server at "db" (172.26.0.2), port 5432 failed: Connection refused
lightchan-web-1 | Is the server running on that host and accepting TCP/IP connections?
lightchan-web-1 |
lightchan-web-1 |
lightchan-web-1 | The above exception was the direct cause of the following exception:
lightchan-web-1 |
lightchan-web-1 | Traceback (most recent call last):
lightchan-web-1 | File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
lightchan-web-1 | self.run()
lightchan-web-1 | File "/usr/local/lib/python3.8/threading.py", line 870, in run
lightchan-web-1 | self._target(*self._args, **self._kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
lightchan-web-1 | fn(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 127, in inner_run
lightchan-web-1 | self.check_migrations()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 505, in check_migrations
lightchan-web-1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
lightchan-web-1 | self.loader = MigrationLoader(self.connection)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
lightchan-web-1 | self.build_graph()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 223, in build_graph
lightchan-web-1 | self.applied_migrations = recorder.applied_migrations()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
lightchan-web-1 | if self.has_table():
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
lightchan-web-1 | with self.connection.cursor() as cursor:
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 270, in cursor
lightchan-web-1 | return self._cursor()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 246, in _cursor
lightchan-web-1 | self.ensure_connection()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
lightchan-web-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 211, in connect
lightchan-web-1 | self.connection = self.get_new_connection(conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
lightchan-web-1 | connection = Database.connect(**conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
lightchan-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
lightchan-web-1 | django.db.utils.OperationalError: connection to server at "db" (172.26.0.2), port 5432 failed: Connection refused
lightchan-web-1 | Is the server running on that host and accepting TCP/IP connections?
lightchan-web-1 |
lightchan-db-1 | running bootstrap script ... ok
lightchan-db-1 | performing post-bootstrap initialization ... ok
lightchan-db-1 | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
lightchan-db-1 | You can change this by editing pg_hba.conf or using the option -A, or
lightchan-db-1 | --auth-local and --auth-host, the next time you run initdb.
lightchan-db-1 | ok
lightchan-db-1 |
lightchan-db-1 |
lightchan-db-1 | Success. You can now start the database server using:
lightchan-db-1 |
lightchan-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
lightchan-db-1 |
lightchan-db-1 | waiting for server to start....2022-03-02 14:21:49.476 UTC [50] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
lightchan-db-1 | 2022-03-02 14:21:49.478 UTC [50] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
lightchan-db-1 | 2022-03-02 14:21:49.498 UTC [51] LOG: database system was shut down at 2022-03-02 14:21:47 UTC
lightchan-db-1 | 2022-03-02 14:21:49.524 UTC [50] LOG: database system is ready to accept connections
lightchan-db-1 | done
lightchan-db-1 | server started
lightchan-db-1 | CREATE DATABASE
lightchan-db-1 |
lightchan-db-1 |
lightchan-db-1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
lightchan-db-1 | psql:/docker-entrypoint-initdb.d/init.sql: error: could not read from input file: Is a directory
lightchan-db-1 exited with code 1
lightchan-db-1 |
lightchan-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
lightchan-db-1 |
lightchan-db-1 | 2022-03-02 14:21:53.927 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
lightchan-db-1 | 2022-03-02 14:21:53.927 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
lightchan-db-1 | 2022-03-02 14:21:53.927 UTC [1] LOG: listening on IPv6 address "::", port 5432
lightchan-db-1 | 2022-03-02 14:21:53.933 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
lightchan-db-1 | 2022-03-02 14:21:53.947 UTC [28] LOG: database system was interrupted; last known up at 2022-03-02 14:21:51 UTC
lightchan-db-1 | 2022-03-02 14:21:56.437 UTC [28] LOG: database system was not properly shut down; automatic recovery in progress
lightchan-db-1 | 2022-03-02 14:21:56.459 UTC [28] LOG: redo starts at 0/16FB5C8
lightchan-db-1 | 2022-03-02 14:21:56.461 UTC [28] LOG: invalid record length at 0/16FB6C0: wanted 24, got 0
lightchan-db-1 | 2022-03-02 14:21:56.461 UTC [28] LOG: redo done at 0/16FB678 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
lightchan-db-1 | 2022-03-02 14:21:56.493 UTC [1] LOG: database system is ready to accept connections
The specific error and why I don't understand the error:
In addition to the above init.sql
error, the waitfor.sh
package is not waiting for the Postgresql file to run. Which is strange as this appears to be the simplest possible example.
tl;dr
Here are two different methods of making docker-compose work on a simple project and neither is working - something appears to be wrong with /docker-entrypoint-initdb.d/init.sql
Thanks for all the help!
Upvotes: 1
Views: 7202
Reputation: 139
Maybe this has already been answered above, but "init.sql" is a very misleading name IF it is used for the directory; because it appears to be just a single SQL initialization script/file. So instead of:
...
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
...
... I would suggest something like the following (from a MySQL DB in Docker Compose env that is IMHO completely analogous):
...
volumes:
- ./database/:/docker-entrypoint-initdb.d
...
AFAIK depends_on
does NOT use healthcheck
ANYMORE (since Docker Compose v3)
And the strongly recommended approach: https://docs.docker.com/compose/startup-order/
The problem of waiting for a database (for example) to be ready is really just a subset of a much larger problem of distributed systems. In production, your database could become unavailable or move hosts at any time. Your application needs to be resilient to these types of failures. To handle this, design your application to attempt to re-establish a connection to the database after a failure. If the application retries the connection, it can eventually connect to the database. The best solution is to perform this check in your application code, both at startup and whenever a connection is lost for any reason.
However, if you don’t need this level of resilience, you can work around the problem with a wrapper script: [...]
Upvotes: 2