postgis / docker-postgis

Docker image for PostGIS
https://hub.docker.com/r/postgis/postgis/
MIT License
1.4k stars 468 forks source link

Error command not found in initdb-postgis.sh #360

Open renjiangyang opened 1 year ago

renjiangyang commented 1 year ago

发现有类似的Issues问题 https://github.com/postgis/docker-postgis/issues/358 我使用的版本是12-3.4 此问题出现在自行构建docker-image的时候, 而通过docker pull拉取的是没有问题的 经过排查 发现initdb-postgis.sh存在问题 如下所示${psql[@]}存在为空的情况, 我不知道这个值是什么时候写入的,固增加前置判断处理

#!/bin/bash

set -e

# Perform all actions as $POSTGRES_USER
export PGUSER="$POSTGRES_USER"

# 这里增加了psql空的处理
if [ -z "${psql[@]}" ]; then
  psql=("psql")
fi

......

烦请作者给出答复是否存在这样的问题,以及这样处理是否合适,感谢

daweimau commented 1 year ago

I have the same issue but cannot see how this is not a dupe of #358

In my case, I copied the Dockerfile (and .sh scripts) contents and recreated the files myself. (I'm doing this to fiddle with the Dockerfile, to troubleshoot another issue).

I am using 15-3.4 alpine, but the initdb script is exactly the same.

The image builds fine. Attempting to run the image yeilds results exactly like OPs -- line 16 of the initdb is failing and the error suggests "${psql[@]}" in this line is evaluating to nothing at all:

# Create the 'template_postgis' template db
"${psql[@]}" <<- 'EOSQL'
CREATE DATABASE template_postgis IS_TEMPLATE true;
EOSQL

# Load PostGIS into both template_database and $POSTGRES_DB
for DB in template_postgis "$POSTGRES_DB"; do
    echo "Loading PostGIS extensions into $DB"
    "${psql[@]}" --dbname="$DB" <<-'EOSQL'
        CREATE EXTENSION IF NOT EXISTS postgis;
/docker-entrypoint-initdb.d/10_postgis.sh: line 16: --dbname=template_postgis: command not found

This is especially baffling because "${psql[@]}" did seem to work at line 9. The CREATE DATABASE template_postgis statement succeeded.

Sorry to say I'm completely woeful with pretty much every tech at play here, and doubt i will be able to help further

daweimau commented 1 year ago

Answer will be in here

daweimau commented 1 year ago

Can be worked around by adding below block near the start of the script

docker_process_sql() {
    local query_runner=( psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --no-psqlrc )
    if [ -n "$POSTGRES_DB" ]; then
        query_runner+=( --dbname "$POSTGRES_DB" )
    fi

    PGHOST= PGHOSTADDR= "${query_runner[@]}" "$@"
}
psql=( docker_process_sql )

This seems to be because the script is using psql which it expects to already be defined by the Postgres entrypoint script. And it is.. But something about the way we are creating this image / Dockerfile is affecting the interaction between the two scripts, and the variable is lost/inaccessible

Workaround: copy the psql definition into this script

ImreSamu commented 1 year ago

Hi @renjiangyang

------ ( translated to english with https://www.deepl.com/ ) ------ Found a similar issue with Issues #358 I'm using version 12-3.4 and this issue occurs when building docker-image by myself, while pulling it via docker pull is fine. After troubleshooting, I found a problem with initdb-postgis.sh. The following ${psql[@]} is empty, I do not know when this value is written, the solid increase in the pre-determination processing

If the image downloaded with docker pull works and your own build does not, the most important thing is not to use "Download ZIP", but instead use git clone https://github.com/postgis/docker-postgis.git to preserve the special Linux file attributes.

And this is also the recommended way in the parent docker-library/postgis repo

"Our stance is generally that if you want to build our images from source, you either need to use git clone to fetch them (which thus gets the appropriate permissions bits on docker-entrypoint.sh) or replicate what git clone would do yourself." via https://github.com/docker-library/postgres/issues/1111#issuecomment-1642661982

The file system is also crucial. For instance, when using Windows, it's advisable to use WSL2 - Ubuntu for local building.

Unfortunately, if you're not using Linux as your operating system, I can't be of much help since I use Ubuntu Linux. And the building of the images also takes place in the cloud under Linux.

devfelip commented 1 year ago

For me it's condition resolved

if [ -z "${psql[@]}" ]; then
  psql=("psql")
fi

Anyway because it happens? Anyone have the answer?

I think and it seems to me variable "${psql[@]}" should exists in another place...

devfelip commented 1 year ago

Update

For me it's condition resolved

if [ -z "${psql[@]}" ]; then
  psql=("psql")
fi

Anyway because it happens? Anyone have the answer?

I think and it seems to me variable "${psql[@]}" should exists in another place...

Update here... Needed permissions in file /docker-entrypoint-initdb.d/10_postgis.sh RUN chmod -x /docker-entrypoint-initdb.d/10_postgis.sh

https://stackoverflow.com/questions/57665146/what-is-the-meaning-of-psql-in-this-script

mbuffa commented 7 months ago

@daweimau Thanks fo the workaround 👌 works perfectly fine. Haven't found a fix yet though.