Kort sagt, dette sker med vilje; forfatteren gør det som en del af initialiseringen.
Det ser ud til, at nogle svar kan findes i billedets entrypoint shell-script:
_main() {
# if first arg looks like a flag, assume we want to run postgres server
if [ "${1:0:1}" = '-' ]; then
set -- postgres "[email protected]"
fi
if [ "$1" = 'postgres' ] && ! _pg_want_help "[email protected]"; then
docker_setup_env
# setup data directories and permissions (when run as root)
docker_create_db_directories
if [ "$(id -u)" = '0' ]; then
# then restart script as postgres user
exec gosu postgres "$BASH_SOURCE" "[email protected]"
fi
# only run initialization on an empty data directory
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
docker_verify_minimum_env
# check dir permissions to reduce likelihood of half-initialized database
ls /docker-entrypoint-initdb.d/ > /dev/null
docker_init_database_dir
pg_setup_hba_conf
# PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless
# e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS
export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}"
docker_temp_server_start "[email protected]"
docker_setup_db
docker_process_init_files /docker-entrypoint-initdb.d/*
docker_temp_server_stop
unset PGPASSWORD
echo
echo 'PostgreSQL init process complete; ready for start up.'
echo
else
echo
echo 'PostgreSQL Database directory appears to contain a database; Skipping initialization'
echo
fi
fi
exec "[email protected]"
}
Med hensyn til "hvorfor?" Jeg tror, det er på grund af ønsket om at køre som en mindre privilegeret bruger.
Du kan "løse" problemet ved at angive en volumen i Compose-filen som sådan:
volumes:
- ./data/pgsql:/var/lib/postgresql/data
Derefter vil den springe rutinen over for at sikre DATABASE_ALREADY_EXISTS
.
Eller, hvis det ikke er nyttigt--du kan grave lidt mere i indgangs-scriptet.