Unable to run a script under the docker-entry-point.d

I’m trying to create a postgis extension into a postgres container.
I create the Dockerfile:
FROM postgres:9.6.4

RUN apt-get clean \
    && apt-get update \
    && apt-get -y install postgresql-9.6-postgis-2.4 postgresql-9.6-postgis-2.4-scripts \
    && rm -rf /var/lib/apt/lists/*

COPY ./scripts/postgis.sh  /docker-entrypoint-initdb.d/postgis.sh

postgis:

    #!/bin/sh

for DB in $(psql -U backend -t -c  "SELECT datname from pg_database where datname = 'backend'"); do
    echo "Loading PostGIS extensions into $DB"
    psql -U backend -d $DB -c "CREATE EXTENSION IF NOT EXISTS postgis";
done

The username and password are defined in the file.yml of kubernetes cluster.
I’m able to create the postgres db and connect to it, the problem that I did’nt found the postgis extension installed. I deleted the image of the postgres db and recreated it but no vain. I didn’t understand what may the problem is? Does the container was not able to execute the postgis script at the begining or does it due to the virtual machine?

if you do
docker logs container id
do you get messages from the entrypoint script??

change line 1 of your script to

#!/bin/bash -x

then u can see the script debug info

might help

Thank you for your answer. I tried into two different machines. In the first the extension was created the successfully and in the second not. I delete the image and recreated it many times and I’m not able to got it. That’s why I want know if there is something that the container take into consideration to run the script or something else prohibt it from executin.

no. the container doesn’t care… as long as the script is marked executable, then it will be executed.

The syntax you’re trying to use here isn’t standard Bourne shell syntax. While there’s a popular GNU variant of the shell with many extensions in widespread use, Docker containers won’t necessarily have it installed, and Debian-based systems won’t have it available as /bin/sh.

If you look at docker logs on your container you’ll probably see a syntax error. Do you have any more information than just “it didn’t work”?

The “How to extend this image” section in the documentation for the standard postgres image has an example that’s a good starting point.

I edited the postgis.sh and I didn’t get any message about the extension in the logs of the container. I’m able to create the extension manually with the script postgis.sh when I executed it inside the container but I didn’t understand why this script was not able to be executed automatically when the container was created.

I updated the psotgis.sh file and when I run the logs of the container I didn’t get the message

Loading PostGIS extensions into $DB

from the script shell.However when I run the script inside the container it works well and the extension was created. I didn’t understand why the script didn’t been executed when the container was created.

do you start in some directory when you do this manually?

is the script on the path?

also, what is docker-entry-point.d?

the dockerfile entrypoint token specifies the application to start at container startup
for example
entrypoint=[docker-entrypoint.sh]

you can see this if you docker inspect the image you are starting

I search through the net and I found that at the beginning docker-entrypoint.sh executes all the scripts under docker-entrypoint.d.The scripts exuctes only the first time when there is no persistant volume data. So should I delete all the data to get the extension created?

I don’t know anything about that