Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres /docker-entrypoint.sh not running scripts in /docker-entrypoint-initdb.d/ #40

Closed
kevinburke opened this issue Dec 13, 2014 · 21 comments

Comments

@kevinburke
Copy link

I'm having trouble writing a script to /docker-entrypoint-initdb.d/ and having Docker run it from /docker-entrypoint.sh.

  • It looks like /docker-entrypoint.sh is supposed to source any scripts it finds in /docker-entrypoint-initdb.d/. I verified I'm running this version of the script by cating the script and verifying this is the expected behavior.
  • I followed the instructions in "justfalter"'s comment here: https://registry.hub.docker.com/_/postgres/ to add a fix-acl.sh file and put it in the correct folder. I double checked the folder name was correct.
  • I also mirrored the commands in the /docker-entrypoint.sh script to check that the script could be sourced and would produce the desired behavior.
  • When I do "docker run", it's clear that Postgres starts but the specified line was not added to pg_hba.conf. I double checked this by verifying the mtime of pg_hba.conf - it wasn't touched or overwritten after my script ran. I also tried having my script touch various files to just verify that it ran, with no success.

I'm at a loss for how to continue debugging this; I placed a file in the place that it's supposed to go, verified that the script runner that starts postgres should run it, and verified that the script does what I want, and I don't see any output (a changed pg_hba.conf, or even a simple file).

I'm running the newest version of everything; starting Docker inside of a Ubuntu Virtualbox being provisioned from Vagrant. I share one folder from the Docker container to the host VM - the data folder containing pg_hba.conf.

Would appreciate your help!
Kevin

@yosifkit
Copy link
Member

Random debug thought: is the script executable in the container, maybe also ownership?

@kevinburke
Copy link
Author

These are both possibilities, though if they were not owned/executable by the right process, it seems from this script that Docker would bail out with a non-zero exit code.

https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh#L48

@jmls
Copy link

jmls commented Feb 23, 2015

I hit this problem - the issue is that the script is only run if there is no postgres data directory ;)

if [ -z "$(ls -A "$PGDATA")" ]; then

I think it would be better to have the script run a "initdb" function for all brand spanking new installations, and a "always run" function

hth

@md5
Copy link
Contributor

md5 commented Feb 23, 2015 via email

@Cactusbone
Copy link

sometime the scripts could be used to perform update operations on the existing data.
for example updating an extension on all affected dbs.
It would be nice to have a base install scripts folder and an update scripts one

@jayfk
Copy link

jayfk commented Jul 17, 2015

I hit this problem - the issue is that the script is only run if there is no postgres data directory ;)

if [ -z "$(ls -A "$PGDATA")" ]; then
I think it would be better to have the script run a "initdb" function for all brand spanking new installations, and a "always run" function

I agree. The current approach makes it extremely hard to supply your own postgresql.conf with a Dockerfile that uses the official postgres image as base.

@yosifkit
Copy link
Member

@jayfk, #72 should fix that part.

@danwyryunq
Copy link

I too met this problem and have been trying to debug it for a week already.
my .sh scripts in docker-entrypoint-init.d/ never run.
I tryed extending Dockerfile from 9.4 image, and also I tryed building it myself from a copy of the original Dockerfile and docker-entrypoint.sh. It seems to be that the docker-entrypoint.sh never gets into the
if [ "$1" = 'postgres' ]; then
condition on line 17 of the script.
I ran out of ideas. Any help on this ?

@yosifkit
Copy link
Member

@danwyry, what are you running? Any information you can give to help us reproduce the issue?

@danwyryunq
Copy link

Sure, this is what I tryied so far:
I downloaded 9.4's Dockerfile and docker-entrypoint.sh to a directory on my computer
I added the functions detailed below to docker-entrypoint.sh and called them right after the set_listen_address '*' function is called.
on my docker-compose.yml I put :

build: my_postgres 
  environment:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
  volumes_from:
   - data
  ports:
   - "5434:5432"

being 'my_postgres' the directory name under which y located the Dockerfile and entrypoint.

I ran docker-compose build.
It ran with no errors, but postgresql.conf.

I performed several hardcoded tests to see where the docker-entrypoint.sh exits and realized itś not even getting past the follwing condition (which in the original file is in line 17)
if [ "$1" = 'postgres' ]; then

These are the functions I added that configure some settings on postgresql.conf (I already tested them on their own and they work ok). :

set_bytea_output() {
    sed -ri '/^[# ]*[# ]*[# ]*bytea_output[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf" 
   echo "bytea_output = '$1'" >> "$PGDATA/postgresql.conf" 

}
set_standard_conforming_strings() {
  sed -ri '/^[# ]*[# ]*[# ]*standard_conforming_strings[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf" 
  echo "standard_conforming_strings = $1" >> "$PGDATA/postgresql.conf" 
}

BTW, I also tryed puting these functions on an alternate script inside docker-entrypoint-init.d and creating a Dockerfile extending from the official image:

FROM postgres:9.4
COPY docker-entrypoint-initdb.d/settings_postgres_conf.sh /docker-entrypoint-initdb.d/settings_postgres_conf.sh
RUN chmod 0755 /docker-entrypoint-initdb.d/settings_postgres_conf.sh

ENV PGDATA /var/lib/postgresql/data
VOLUME /var/lib/postgresql/data

But it won't work either.

@yosifkit
Copy link
Member

Hmmm... I am unable to reproduce. I started with the docker-entrypoint.sh and added your functions with some output to see it works. I guessed on the values to apply.

diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh
index 87d7e3b..3334092 100755
--- a/docker-entrypoint.sh
+++ b/docker-entrypoint.sh
@@ -6,6 +6,16 @@ set_listen_addresses() {
        sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
 }

+set_bytea_output() {
+       sed -ri '/^[# ]*[# ]*[# ]*bytea_output[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf"
+       echo "bytea_output = '$1'" >> "$PGDATA/postgresql.conf"
+}
+set_standard_conforming_strings() {
+       sed -ri '/^[# ]*[# ]*[# ]*standard_conforming_strings[ ]*=[ ]*.*/d' "$PGDATA/postgresql.conf"
+       echo "standard_conforming_strings = $1" >> "$PGDATA/postgresql.conf"
+}
+
 if [ "$1" = 'postgres' ]; then
        mkdir -p "$PGDATA"
        chown -R postgres "$PGDATA"
@@ -84,6 +94,13 @@ if [ "$1" = 'postgres' ]; then

                gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
                set_listen_addresses '*'
+               set_bytea_output 'hex'
+               set_standard_conforming_strings 'off'
+               echo '*****************************'
+               echo '*****************************'
+               cat "$PGDATA/postgresql.conf"
+               echo '*****************************'
+               echo '*****************************'

                echo
                echo 'PostgreSQL init process complete; ready for start up.'

Then I ran postgres with the modified entrypoint:

$ docker run -it --rm -v ~/postgres/modified-docker-entrypoint.sh:/docker-entrypoint
.sh:ro -e POSTGRES_PASSWORD=password postgres:9.4

@danwyryunq
Copy link

would you mind trying it within a docker-compose configuration to see if maybe that's making any difference?

@mericano1
Copy link

Hi @danwyry did you ever get your scripts running? I am having the same issue using docker-compose

@danwyryunq
Copy link

nope, couldn't do it. But I stopped trying after a while

@yosifkit
Copy link
Member

@danwyry, my newest idea is that the volume was persisting. With docker compose it tries really hard to keep volumes (ie your initialized database files). Most of the entrypoint script only runs on first initialization. docker-compose rm -v should get rid of all the containers and their volumes if you want to start fresh on the database.

@brendan-rius
Copy link

Any ETA on when we would be able to easily re-launch the init script (via a command maybe?), without removing the volumes?

@blag
Copy link

blag commented Sep 25, 2016

I had this exact same issue and it was solved by removing the volume associated with the container in docker-compose.yml.

@brendan-rius If you just need the data saved in the volume (and you don't have anything else stored there), you should be able to run your postgres container (see the "... or via psql" section here) manually, dump your database, remove the volume, recreate everything, and import the database again.

@ekimia
Copy link

ekimia commented Sep 28, 2016

is there an easy way to just set all listen addresses so my code can access postgres? My code isnt copied into the image in development mode.

@yosifkit
Copy link
Member

@ekimia, what do you mean? If you have any shell scripts in /docker-entrypoint-initdb.d/ then they can access postgres via localhost.

If you are talking about accessing postgres from another container, then your code will have to wait for postgres to be ready, after it has done its initialization and run through any initdb scripts.

@yosifkit
Copy link
Member

I feel like the original issue here has been solved. If you are still having trouble getting /docker-entrypoint-initdb.d/ scripts to run, please open a new issue with as much information as possible.

@wyuenho
Copy link

wyuenho commented Dec 15, 2016

I just ran into this problem. I feel the initdb scripts will only run when the volume is first created should be made clear in the image description here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests